23
What is the real use of using the reserved word this
?
As far as I can see it makes use or not, but I doubt its functionality.
23
What is the real use of using the reserved word this
?
As far as I can see it makes use or not, but I doubt its functionality.
32
The this
serves to reference the object itself.
There are cases where its use becomes necessary.
class Pessoa {
private string nome;
public Pessoa(string nome){
this.nome = nome;
}
}
In the example above if I didn’t use the this
, would be referring to the parameter and not to the class name property.
And others where its use becomes optional:
class Pessoa {
private string _nome;
public Pessoa(string nome){
// como não existe nenhuma variável "_nome" neste escopo, ele vai buscar o atributo da classe
_nome = nome;
}
}
17
It depends on where. In many places it really doesn’t need to be used. But if there is some ambiguity (same name) between the local variable and the instance variable, how will you know which of the two to use?
In these cases it is fundamental to say that it is the instance variable.
class Teste {
private int x = 0;
private int y = 1;
public void Exemplo(int x) {
WriteLine(x); //imprime o parâmetro
WriteLine(y); //imprime a variável de instância, não tem ambiguidade
WriteLine(this.x); //imprime a variável de instância
}
}
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
The this
at the bottom is a hidden parameter that every non-static method receives. So when accessing this.x
is accessing the member x
of the local variable called this
which was received by a parameter that you do not see. It is not something so magical that the language creates. The magic occurs only in the insertion of this hidden parameter. What the compiler does is infer that it is referring to this
when trying to access a variable that does not exist locally in the method, but exists in the instance. He can safely say that this is what it is, then he "magically" inserts the this
for you in front of the member.
If you have it compiled see that the generated code puts the name of the class in front of the y
, even if the code does not. It is the only way to disambiguate at the end:
.method public hidebysig instance void
Exemplo(int32 x) cil managed {
.maxstack 8
IL_0000: nop
IL_0001: ldarg.1
IL_0002: call void [mscorlib]System.Console::WriteLine(int32)
IL_0007: nop
IL_0008: ldarg.0
IL_0009: ldfld int32 Teste::y // <========================== veja aqui
IL_000e: call void [mscorlib]System.Console::WriteLine(int32)
IL_0013: nop
IL_0014: ldarg.0
IL_0015: ldfld int32 Teste::x
IL_001a: call void [mscorlib]System.Console::WriteLine(int32)
IL_001f: nop
IL_0020: ret
} // end of method Teste::Exemplo
So @bigown I think I understand, in this your example the line WriteLine(this.x);
be referencing the variable private int x = 0;
and not to the parameter being passed.
Exactly. When you don’t have that need, you don’t need to use.
7
The reserved word this represents the running object. With it, implicitly you call the object being used.
Example:
class Classe{
private int numero;
Classe(int numero){
this.numero = numero; // Aqui você faz referência à propriedade "numero" da classe "Classe"
}
}
Browser other questions tagged c# .net oop
You are not signed in. Login or sign up in order to post.
I usually use the
this
even when the use is optional to make the code more readable and quickly understand that it is referencing a class attribute.– dellasavia
In java is similar, I rarely used
this.
, but today I use it for the reason I mentioned and to facilitate "identifying" the variables of the "object" +1– Guilherme Nascimento