4
In the early days of my first object orientation classes I was taught to use the this
where it is a variable or class property.
Today I understand when there is a real need to use, for example when a parameter is received in the method that has the same name as a class variable:
private string algo;
public void FazerAlgo(string algo)
{
this.algo = algo;
}
Here answers that too, but that’s not the question yet.
I noticed that now in Visual 2015 keeps saying that the use of this
in situations other than the above is redundant. This did not happen in other versions as far as I can remember.
I always prefer to wear the this
, I think it makes the code more readable and I got used to it like this, but I had not attempted it until then to the fact of ambiguity, so I’m starting to change. For the question not to be subjective I ask:
Is there any reference/coding standard that always recommends the use of this
where my teacher might have taken this?
It wouldn’t be because you can use the value
algo
directly, without using assign it to anything else?– Wallace Maxters
@Murilo, the answers below already say a lot but, about C#, has languages that use
this
is mandatory, for examplePHP
. I just wanted to make that reservation!– novic