CSharp/C# Tutorial - C# this Reference






The this reference refers to the instance itself.

In the following example, the Assign method uses this to set the manager's Manager field:


public class Person { 
    public Person Manager;
    public void Assign (Person p) { 
       Manager = p; 
       p.Manager = this;
 } 
} 

The this reference also disambiguates a local variable or parameter from a field.

For example:


public class Test {
   string name;
   public Test (string name) { 
      this.name = name; 
   } 
} 

The this reference is valid only within nonstatic members of a class or struct.