this keyword: The keyword "this" in C# is used to access the field variables. All the field variables must be accessed using "this" keyword.
It is useful when the field variable and the local variables of a method have same name.
In the above case local variables overrides field variables and field variables are no more visible inside the method. So this can be used to refer field variables to make visible inside method.
See the following code
class Demo Output:
public void Get()
Console.WriteLine("It is the class variable");
Console.WriteLine("Now the local variables are:");
static void Main(string args)
Demo d= new Demo();
It is the class variable
Now the local variables are:
When we write this.a and this.b, it will refer to the field variables. But when we write only a and b it refers to the local variables of the method Get().
As the field variable and local variables have the same name we have to use the this keyword.We can't access a field variable without this keyword in C#. this always refer to the members of same class.