C# this Keyword
The “this” keyword is a special type of reference variable, that is implicitly defined within each constructor and non-static method as a first parameter of the type class in which it is defined.
![](https://www.c-sharpcorner.com/UploadFile/891f80/this-keyword-in-C-Sharp/Images/This%20keyword.jpg)
1.It is used to refer to the current instance of the class .
2. Also used to differentiate between the method parameters and class fields if they both have the same name. OR distinguish instance variables of the class and formal parameters defined in a constructor call.
Lets See the Example
using System.IO; using System; class Student { public int id, age; public String name, subject; public Student(int id, String name, int age, String subject) { this.id = id; this.name = name; this.subject = subject; this.age = age; } public void showInfo() { Console.WriteLine(id + " " + name+" "+age+ " "+subject); } } class StudentDetails { public static void Main(string[] args) { Student std1 = new Student(001, "Jack", 23, "Maths"); Student std2 = new Student(002, "Harry", 27, "Science"); Student std3 = new Student(003, "Steve", 23, "Programming"); Student std4 = new Student(004, "David", 27, "English"); std1.showInfo(); std2.showInfo(); std3.showInfo(); std4.showInfo(); } }
3. Another usage of “this” keyword is to call another constructor from a constructor in the same class.
4. It can be used to pass current object as a parameter to another method.
using System; namespace OOP { //Start of class Employee public class Employee { public int id, age; public string name; public float salary; public Employee(int id, int age, string name, float salary) { this.id = id; this.name = name; this.age = age; this.salary = salary; } public void display() { Display.display(this); Console.WriteLine("printing inside employee class...."); Console.WriteLine(id + " " + name + " " + salary + " " + age); } public static void Main(string[] args) { Employee emp = new Employee(101, 24, "Ravi malik", 25000); emp.display(); } }//End of class Employee Which is main class for the program //Start Of Class Disaplay public class Display { public static void display(Employee emp) { Console.WriteLine("printing inside Display class… Get Data from a C# object or instance through Passing 'this' in a method "); Console.WriteLine(emp.id + " " + emp.name + " " + emp.salary + " " + emp.age); } } //End of Class of Display } //End of NameSpace /*Out Put of the Program*/ /* printing inside Display clas... 101 Ravi malik 25000 24 printing inside employee class.... 101 Ravi malik 25000 24 */
Output
printing inside Display clas... 101 Ravi malik 25000 24 printing inside employee class.... 101 Ravi malik 25000 24
5. It can be used to declare indexers
6. this keyword can be used to calling current class method
using System; public class ThisExample2 { public void m() { Console.WriteLine("Hey You Are in M Method"); } public void n() { this.m();//m() calling current class method Console.WriteLine("Hey you Are in N method"); } public static void Main(string[] args) { ThisExample2 t1=new ThisExample2(); t1.n(); } }
Output :
Hey You Are in M Method Hey you Are in N method