In c#, this keyword is used to refer to the current instance of a class, and by using
this keyword, we can pass a current instance of the class as a parameter to the other methods.
In case if the class contains parameters and variables with the same name, then
this keyword is useful to distinguish between the parameters and variables.
We can also use
this keyword to declare indexers and to specify the instance variable in the parameter list of an extension method.
Following is the syntax of using
this keyword in c# programming language.
If you observe the above syntax,
this is a keyword, and instance_variable is an instance variable name.
Following is the example of using
this keyword in c# programming language to refer to the class variables and parameters of the same name and use this keyword to send an instance of the class to another class's method.
If you observe the above example, we used
this keyword to distinguish between class variables and parameters of the same name and used
this keyword to send an instance of a class (User) to the method of another class.
When we run the above c# program, we will get the result as shown below.
This is how we can use
this keyword in c# programming language to refer to the instance of a class based on our requirements.