
Two variables sometimes have the same identifier. A field can be named the same as a formal parameter. With the this keyword, we indicate the class member with that identifier. The this keyword is part of an instance expression and it eliminates naming conflicts. Alone it indicates the current type instance.
KeywordsThis C# article uses the this keyword. The this keyword is part of an instance expression.

This program shows how you can use the "this" keyword when referring to a class instance. It shows that you can use the "this" keyword inside the class declaration only and to refer to that instance. "This" cannot refer to a static field or method. It cannot occur inside a static class. The "this" keyword is inferred by the compiler and not required. It is useful for expressing your intent at the level of the source code.
Static ModifierProgram that uses "this" instance expression [C#]
using System;
class Perl
{
public Perl()
{
// Call instance method with this.
this.B();
}
int _a; // Instance field
public void B() // Parameterless instance method
{
// Increment instance field without "this".
_a++;
// Increment instance field with "this".
this._a++;
// Read instance field with "this".
Console.WriteLine("B called: " + this._a);
}
}
class Program
{
static void Main()
{
// Create a new instance of the type Perl.
// ... The constructor calls method B.
// ... Then we call method B again.
Perl perl = new Perl();
perl.B();
}
}
Output
B called: 2
B called: 4Instance method. This program demonstrates how you can call an instance method with the "this" instance expression in the C# language. The method B can be called as "this.B()" if you are inside the class only. If you are external to the class, you can call B as "perl.B()", where "perl" is the instance variable identifier.
Field. The program also shows how you can access a field with the instance expression "this". The statements "_a++" and "this._a++" are precisely equivalent in this program, because the compiler will automatically resolve the instance expression in each case. You can use "this" to disambiguate the variable, for times when you have a conflict in a parameter or local variable name and a field name.
Note: The term "precisely equivalent" means exactly equal. The term "disambiguate" means to make the difference clear.

As you read the C# specification, each member type describes its behavior with the "this" keyword as an instance expression. None of the member types allow you to use "this" when dealing with a static declaration. The standard refers to the "this" keyword as the instance expression. The standard goes to great detail about the nature of expressions in a 150-page chapter.
The C# Programming Language: SpecificationNote: The Expressions chapter was not my favorite chapter in the specification.
There are other contexts and locations in source text in the C# language that you can use the keyword 'this'. For example, you can use 'this' in a constructor initializer to implement constructor overloading and code sharing. Also, you can use 'this' to declare an indexer. An indexer can be accessed with the same syntax as an array type.
This Constructor Initializer Indexer Examples
Extension method syntax uses the "this" keyword in a different context. "This" is used to specify the instance variable in the parameter list of the extension method.
Extension Method
We looked at the "this" instance expression in the C# language. We provided details from the C# standard itself. The instance expression is optional in many cases. It indicates exactly which identifier you are referring to by providing the context.
Class Examples