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.

In this above example,
  • 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.

this_keyword_sample_1
this_keyword_sample_1

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.

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.

this_keyword_sample_2
this_keyword_sample_2

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.

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.

 In this example,
  • 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.

this_keyword_sample_3
this_keyword_sample_3

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.