this Reference - CSharp Custom Type

CSharp examples for Custom Type:this

Introduction

The this reference refers to the instance itself.

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

Marry method uses this to set the partner's mate field:

public class Animal
{
  public Animal Mate;

  public void Marry (Animal partner)
  {
    Mate = partner;
    partner.Mate = 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; }
}

Related Tutorials