find and solve ||
Please wait.....

C# | this Keyword

In c#, this keyword refers to the current instance of the class. This keyword refers to the current instance of the class and is also used as a modifier of the first parameter of an extension method.

There can be 3 main usages of this keyword in C#.

  • It can be used to refer current class instance variable. It is used if field names (instance variables) and parameter names are the same, that is why both can be distinguished easily.
  • It can be used to pass a current object as a parameter to another method.
  • It can be used to declare indexers.

C# this example

   using System;  
   public class EmployeeInfo
        public int Id;
        public String Name;
        public int Age;
        public Employee(int id, string name,int age)
            this.Id= id;
            this.Name= name;
            this.Age= age;
        }         public void display()
            Console.WriteLine(Id + " " + Name+" " + Age);
   public class AnotherClassEmployeeInfo{
       public static void Main(string[] args)
            EmployeeInfo obj = new EmployeeInfo(1001, "Sylvia",38);
            EmployeeInfo obj1 = new EmployeeInfo(1002, "Rahul", 27);             obj.display();


1001 Sylvia 38

1002 Rahul 27