Algorithm


In C#, the this keyword is used to refer to the current instance of the class. It is a reference to the current object on which a method or a property is being invoked. The this keyword is particularly useful in scenarios where there might be a naming conflict between instance variables and method parameters or local variables.

Here are some common uses of the this keyword in C#:

 

Code Examples

#1 C# Programming this Keyword

Code - C# Programming

using System;
 
namespace ThisKeyword {
  class Test {

    int num;
    Test(int num) {
      // this.num refers to the instance field
      this.num = num;
      Console.WriteLine("object of this: " + this);
    }

    static void Main(string[] args) {

      Test t1 = new Test(4);
      Console.WriteLine("object of t1: " + t1);
      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
object of this: ThisKeyword.Test
object of t1: ThisKeyword.Test

#2 this with Same Name Variables in C# Programming

Code - C# Programming

using System;
 
namespace ThisKeyword {
  class Test {

    int num;
    Test(int num) {

      num = num;
    }

    static void Main(string[] args) {

      Test t1 = new Test(4);
      Console.WriteLine("value of num: " + t1.num);
      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

#3 Code Example- this with Same Name Variables

Code - C# Programming

using System;
 
namespace ThisKeyword {
  class Test {

    int num;
    Test(int num) {
      
      // this.num refers to the instance field
      this.num = num;
    }

    static void Main(string[] args) {

      Test t1 = new Test(4);
      Console.WriteLine("value of num: " +t1.num);
      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
value of num: 4

#4 Invoke Constructor of the Same Class Using this in C# Programming

Code - C# Programming

using System;
 
namespace ThisKeyword {
  class Test {
    
    Test(int num1, int num2) {

      Console.WriteLine("Constructor with two parameter");
    }
    
    // invokes the constructor with 2 parameters
    Test(int num) : this(33, 22) {

      Console.WriteLine("Constructor with one parameter");
    }

    public static void Main(String[] args) {

      Test t1 = new Test(11); 
      Console.ReadLine();   
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Constructor with two parameter
Constructor with one parameter

#5 C# Programming this as an object argument

Code - C# Programming

using System;
 
namespace ThisKeyword {
  class Test {
    int num1;
    int num2;
      
    Test() {
      num1 = 22;
      num2 = 33;
    }

    // method that accepts this as argument   
    void passParameter(Test t1) {
      Console.WriteLine("num1: " + num1);
      Console.WriteLine("num2: " + num2);
    }

    void display() {
      // passing this as a parameter
      passParameter(this);
    }
  
    public static void Main(String[] args) {
      Test t1 = new Test();
      t1.display();
      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
num1: 22
num2: 33

#6 this to declare a C# Programming indexer

Code - C# Programming

using System;
namespace ThisKeyword {
      
  class Student {
      
    private string[] name = new string[3];
  
    // declaring an indexer
    public string this[int index] {

      // returns value of array element
      get {
        return name[index];
      }
      
      // sets value of array element
      set { 
        name[index] = value;
      }
    }
  }
  
  class Program {
  
    public static void Main() {
      Student s1 = new Student();
      s1[0] = "Akash";
      s1[1] = "Rony";
      s1[2] = "Jony";

      for (int i = 0; i  <  3; i++) {

        Console.WriteLine(s1[i] + " ");
      }
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Akash
Rony
Jony
Advertisements

Demonstration


C# Programming this Keyword