"This" Keyword in C#

"This" is one of the keywords in C# which exposes some beautiful functionalities for users. Let’s discus a few of them.

Use of "This" Keyword inside a function:

To understand use of this keyword inside a function, lets create a custom class named “MyCustomClass”.

MyCustomClass

In the above example we are trying to print the value of ‘this’ inside ThisKeyWordFunctionality (). For obvious reason, we don’t have any compile time errors. On the output screen we see “TestApp.MyCustomClass.” So, we are seeing Class type as an output. It means ‘this’ keyword actually holds a reference of the class object inside an instance function.

Case-Study:

If ‘this’ keyword holds a reference of the class, then it can have access to class members/functions. The answer is yes, it does have access to the class members/functions. Let’s see it in example.

this

This time we did create two more functions to the same program. Newly created functions are named as “InstanceFunctionForDemo()” & “StaticFunction()”

We are also trying to access the functions declared inside the same class. On screen we can see static function is not available, however all instance function is available for use. This is pretty much clear. To access static method we don’t really need any instance of the class. It can be invoked directly using the class name.

class

Friends, can you guess the output? If your answer resembles the below then you are right.

Output:

This is instance Function
InstanceFunctionForDemo

Analysis:

InsideMain() we are calling ThisKeyWordFunctionality(). It internally calls InstanceFunctionForDemo. So, “This is instance Function” would be printed on to screen. Since InstanceFunctionForDemo() is returning string of value “InstanceFunctionForDemo” – So on next line, InstanceFunctionForDemo would be printed.

class

Case-Study:

Can we use this keyword inside static function? Can we have this keyword applicable to static classes also?

For both the questions, the answer is NO. For static function we don’t see the option to use ‘this’ keyword. If we want to use any non-static/instance member inside a static function, we have to follow the traditional approach of creating the instance of the same class.

class

If we want to call Instance methods inside static function, follow as shown below:

function

Let’s call the static method in Main(), to see the Output.

output

Output:

output

However, if we do understand the concept of static class clearly then we cannot create the instance of a static class. So, exposing this keyword inside static class is meaningless. It means, if you would try to type “this” inside a static class, it would never be available to the user.

test

This Keyword for constructor chaining:

constructor

In our example, we have instantiated the object by statement “new ConstructorChain ()”.

ConstructorChain () isthe default constructor. Here we have used “: this ()”. This is the approach to chain constructors. Since we have three constructors available in ConstructorChainclass we can have the option to call any one of the constructors, before default constructor would execute. Therefore, if you are guessing the output to be as below you are definitely right to grab the concept.

program

Let’s see if we have the option to chain all the constructor. Here is one of the approach to chaining multiple constructors in a class.

class

The only change is we have appended following code to the parameterized constructor holding int type parameter.

  1. public ConstructorChain (int inputStr)  
  2. this ("parameterized constructor")  
Case-Study:

When should we use constructor chaining? Can we have an example?

Sure, let’s add a property to “ConstrutorChain” class. Make the default constructor as private. Now we don’t want the create the object of class using default constructor. However, we want all the properties to get initialized with defined values only inside a private constructor. We can proceed with the following approach.

class

This Keyword for defining Extension Methods:

method

In the above example we can see This keyword has been used in 1st parameter while defining the function. In Extension methods, the 1st parameter is always used along with This keyword and parameter/argument type defines the extension to which type. In our case Str is of string type. So, ConvertFirstWordToUpper () is extension to string type.

Conclusion:

We have seen the following with a few practical implementations.

This keyword inside a function.

This Keyword as Constructor Chaining.

This keyword as Parameter to ExtensionMethods.

Read more articles on C#: