In C#, this keyword mostly used to refer the current instance of a class or struct. It is also used to access members (fields, properties, methods, etc.) and invoke constructors within the current class. In this blog we will explore it in detail.
Various ways of using this keyword in C#:
In C#, the “this” keyword has several uses, and its behavior can vary depending on the situation in which it is used.
Here are the various ways you can use the “this” keyword in C#:
-
- Accessing Class members
- Invoking Constructors
- Passing the Current Instance
- Extension methods
Let’s explore each implementation in detail with example.
Accessing Class members using this :
You can use this keyword to access members of the current instance of a class or struct, use of this is implicit when calling any instance member, and it returns an instance of the object itself.
It is particularly useful when a member variable or parameter name conflicts with a local variable or parameter.
Let’s have a look at below example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System; namespace UseOfThisKeywordDemo { class Student { //Instance varibale public string FirstName; public string LastName; public void SetNewName(string FirstName, string LastName) { //Accessing the instance variables using this keyword this.FirstName = FirstName; this.LastName = LastName; } public string GetFullName() { return $"{FirstName} {LastName}"; } } class Program { static void Main(string[] args) { Student objStudent = new Student(); //Call the method to set the instance variable value objStudent.SetNewName("Toney", "Shark"); //Print the output Console.WriteLine("Full Name: " + objStudent.GetFullName()); } } } |
- The method SetNewName takes FirstName and LastName as parameter, which shadows the class instance variable field with the same name.
- By using this.FirstName and this.LastName, we explicitly refer to the instance field, allowing us to assign the parameter value to it.
- The GetFullName method returns the concatenated value of the instance field.
When we run the above program then it generates below output.

Invoking Constructors using this:
We can use this keyword to invoke other constructors within the same class, which is known as constructor chaining.
let’s have a look at below example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System; namespace UseOfThisKeywordDemo { class Employee { //Instance varibale private int Salary; public Employee():this(0) { } public Employee(int intSalary) { //Accessing the instance variables using this keyword this.Salary = intSalary; } public int GetSalary() { return this.Salary; } } class Program { static void Main(string[] args) { Employee emp1 = new Employee(); int firstSalary = emp1.GetSalary(); Console.WriteLine("Employee First Salary : " + firstSalary); Employee emp2 = new Employee(10000); int secondSalary = emp2.GetSalary(); Console.WriteLine("Employee Second Salary : " + secondSalary); } } } |
In this above example,
- The Employee class has two constructors. The parameter less constructor uses this(0) to invoke the constructor with the int parameter and pass a default value of 0.
- The second constructor assigns the provided value to the instance field Salary. By using constructor chaining, we can create objects with different initial field values.
When we run the above program then it generates below output.

Passing the Current Instance using this keyword:
We can use this keyword to pass the current instance of a class to other methods or constructors. It is commonly used when subscribing to events or passing the instance to another object.
1 2 3 4 5 6 7 8 9 10 |
public class MyDemoClass { public event EventHandler MyDemoEvent; public void DoSomething() { //Passes the current instance as the sender MyDemoEvent?.Invoke(this, EventArgs.Empty); } } |
In this example, the DoSomething method invokes the MyDemoEvent event and passes the current instance ( this) as the sender. This allows subscribers to identify the source of the event.
Use of this keyword in Extension methods:
this keyword is also used when defining extension methods to indicate the type that the method extends.
Lets create an extension method to see its usage.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; namespace UseOfThisKeywordDemo { public static class StringExtensions { public static bool IsStringLonger(this string inputText, int length) { return inputText.Length > length; } } public class Program { public static void Main() { string inputString = "Hello, World!"; bool isStingLonger = inputString.IsStringLonger(10); Console.WriteLine($"Is the input text longer than 10 characters? {isStingLonger}"); } } } |
- We define an extension method IsStringLonger for the string type. The this keyword in the method signature indicates that this method extends the string type.
- The method checks if the length of the given text is greater than the specified limit and returns the result.
When we run the above program then it generates below output.

Conclusion:
The this keyword is a powerful tool for working with instance members, invoking constructors, and passing the current instance in C#. It helps clarify the code and avoid naming conflicts within a class. By understanding and using this keyword effectively, you can enhance the readability and maintainability of your code.