Home
C#
this Keyword
Updated Mar 6, 2025
Dot Net Perls
This. Here we eliminate naming conflicts in our C# programs. The "this" keyword indicates the current instance. Two variables sometimes have the same identifier.
A special keyword (this) eliminates mistakes—it indicates the class member with the identifier. It can also be part of an indexer on a class (this is a separate thing).
Indexer
Constructor
Extension
Example. This program uses the "this" keyword when referring to a class instance. We use the "this" keyword inside the class declaration only and to refer to that instance.
Tip The keyword "this" can not refer to a static field or method. It cannot occur inside a static class.
Tip 2 The "this" keyword is inferred by the compiler and not required. It is useful for expressing your intent.
Part 1 B is an instance method. The method B can be called as "this.B()" only if you are inside the class.
Part 2 The program accesses a field with "this". The statements "_a++" and "this._a++" are equivalent in this program.
using System; class Perl { public Perl() { // Part 1: call instance method with "this." this.B(); } int _a; // Instance field public void B() { // Part 2: increment instance field without "this." _a++; // ... Use this. this._a++; // ... Read instance field. 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(); } }
B called: 2 B called: 4
Argument example. We can pass the "this" instance to another method. That method will then receive the reference to the class we called it from.
Tip This approach can be useful in some contexts where you have a chain of method calls and constructors.
using System; class Net { public string Name { get; set; } public Net(Perl perl) { // Use name from Perl instance. this.Name = perl.Name; } } class Perl { public string Name { get; set; } public Perl(string name) { this.Name = name; // Pass this instance as a parameter! Net net = new Net(this); // The Net instance now has the same name. Console.WriteLine(net.Name); } } class Program { static void Main() { Perl perl = new Perl("Sam"); } }
Sam
Constructor initializer. You can use "this" in a constructor initializer to implement constructor overloading and code sharing. It makes classes simpler.
class
Indexer. We can also use "this" to declare an indexer. An indexer can be accessed with the same syntax as an array type. We can have expression-bodied indexers.
Extension method syntax uses the "this" keyword in a different context. We use "this" to specify the instance variable in the parameter list of the extension method.
To understand this keyword, we provided details from the C# standard itself. The instance expression is optional in many cases. It indicates which identifier is being referenced.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Mar 6, 2025 (simplify).
Home
Changes
© 2007-2025 Sam Allen