3
Observe 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 the following two ways, I noticed that the program returns the same value when the class is instantiated. So I’d like to know the difference between using:
int porcento = (this.marks * 100) / Program.maxmarx;
or
int porcento = (this.marks * 100) / maxmarx;
Here is your answer: https://answall.com/questions/54012/qual-a-fun%C3%A7%C3%A3o-de-um-m%C3%A9todo-est%C3%A1tico
– Thiago Araújo
I believe that for this example there is no difference, but the first mode allows you to differentiate the class attribute from a local variable if there is a conflict of names in the scope. For example, if the local variable exists in this method
int maxmarx = 1
, the first mode will work perfectly, while the second will produce an "unexpected".– Woss
The second answer deals specifically with the subject addressed in this question. If you think not, let me know.
– Jéf Bueno
I would write
var porcento = marks * 100 / maxmarx;
, if the variable were used elsewhere, otherwise neither would exist.– Maniero
So, Anderson, she mentions the value declared in the local variable of the Program class. If I need to reference the value 50, determined in the class creation, I use the Program. However, if I wish to use a value other than 50, I should call it without the Program., due to the value with which I want to work be different?
– Mateus Binatti