Doubt in method declaration [duplicate]

3

Note the following code:

 class Program
{
    int marks;
    static int maxmarx = 50;
    void CalcularPorcentagem()
    {
        int porcento = (this.marks * 100) / Program.maxmarx;

        Console.WriteLine(porcento);

    }

After testing in the following two ways, I noticed that the program returns the same value when the class is instantiated. So I would like to know the difference between using:

     int porcento = (this.marks * 100) / Program.maxmarx;

or

     int porcento = (this.marks * 100) / maxmarx;
    
asked by anonymous 26.07.2017 / 15:16

2 answers

1

You are inside class Program referencing static variable maxmarx ; that is, within the class it sees the variable without needing to reference the class.

So both Program.maxmarx and maxmarx have the same result.

    
26.07.2017 / 15:23
1
  

So I would like to know the difference between using

As it is presented, within a block of code (method) where there is no other variable called maxmarx , none.

Now, if you had a local variable called maxmarx , then yes, to reference the static variable it is mandatory to reference using the class name ( Program.maxmarx ).

Out of this scenario (local variable with the same name), refer to the appended form or not at the developer / development team's discretion.

It is the same discussion between referencing an instance variable with this or not. For example: this.marks * 100 or marks * 100 ? The result is the same.

Typically, this "coding type" is discussed / discussed at the beginning of the project by the development team, so that all developers work in the same way.

    
26.07.2017 / 15:26