C# | this Keyword
this keyword is used to refer to the current instance of the class. It is used to access members from the constructors, instance methods, and instance accessors. this keyword is also used to track the instance which is invoked to perform some calculation or further processing related to that instance. Following are the different ways to use ‘this’ keyword in C# :
Program 1: Using ‘this’ keyword to refer current class instance members
// C# program for using 'this' keyword to // refer current class instance members using System; namespace geeksforgeeks { class Geeks { // instance member public string Name; public string GetName() { return Name; } public void SetName(string Name) { // referring to current instance //"this.Name" refers to class member this.Name = Name; } } class program { // Main Method public static void Main() { Geeks obj = new Geeks(); obj.SetName("Geeksforgeeks"); Console.WriteLine(obj.GetName()); } } } |
Geeksforgeeks
Program 2 : Using this() to invoke the constructor in same class
// C# program for using 'this' keyword to // invoke the constructor in same class using System; namespace geeksforgeeks { class Geeks { // calling another constructor // calls Geeks(string Name) // before Geeks() public Geeks() : this("geeks") { Console.WriteLine("Non-Parameter Constructer Called"); } // Declare Parameter Constructer public Geeks(string Name) { Console.WriteLine("Parameter Constructer Called"); } } class program { // Main Method public static void Main() { Geeks obj = new Geeks(); // Warning: obj never used.. } } } |
Parameter Constructer Called Non-Parameter Constructer Called
Program 3: Using ‘this’ keyword to invoke current class method
// C# code for using this to invoke current // class method using System; class Test { void display() { // calling fuction show() this.show(); Console.WriteLine("Inside display function"); } void show() { Console.WriteLine("Inside show funcion"); } // Main Method public static void Main(String []args) { Test t1 = new Test(); t1.display(); } } |
Inside show funcion Inside display function
Program 4: Using ‘this’ keyword as method parameter
// C# code for using 'this' // keyword as method parameter using System; class Test { int a; int b; // Default constructor Test() { a = 10; b = 20; } // Method that receives 'this' // keyword as parameter void display(Test obj) { Console.WriteLine("a = " + a + " b = " + b); } // Method that returns current // class instance void get() { display(this); } // Main Method public static void Main(String[] args) { Test obj = new Test(); obj.get(); } } |
a = 10 b = 20
Program 5: Using this keyword to declare an indexer
// C# code for using 'this' // keyword to declare indexer using System; namespace geeksforgeeks { class Geeks { private string[] days = new string[7]; // declaring an indexer public string this[int index] { get { return days[index]; } set { days[index] = value; } } } class Program { // Main Method public static void Main() { Geeks g = new Geeks(); g[0] = "Sun"; g[1] = "Mon"; g[2] = "Tue"; g[3] = "Wed"; g[4] = "Thu"; g[5] = "Fri"; g[6] = "Sat"; for (int i = 0; i < 7; i++) Console.Write(g[i] + " "); } } } |
Sun Mon Tue Wed Thu Fri Sat
Recommended Posts:
- C# | as Operator Keyword
- C# | is Operator Keyword
- Static keyword in C#
- Is vs As operator keyword in C#
- C# | finally keyword
- LINQ | Let Keyword
- typeof Operator Keyword in C#
- How to implement is functionality without using is keyword in C#
- Invoking an overloaded constructor using this keyword in C#
- Difference between readonly and const keyword in C#
- Range Structure in C# 8.0
- Basics Operations of File and Directory in C#
- Check if the given ranges are equal or not in C#
- How to Create a Range to a Specified End in C#?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



