The this keyword is used to refer to the current object of a class. It helps access the current object's fields, properties, methods, and constructors.
In this chapter, you will learn what the "this" keyword is, its syntax, common uses, and how to use it in C# with examples.
The "this" keyword refers to the current instance of a class. It is used to access instance members such as fields, properties, and methods of the current object.
The "this" keyword is especially useful when a constructor or method parameter has the same name as a class field. In such cases, it helps distinguish the class member from the local parameter.
The following syntax is commonly used with the this keyword:
In this syntax,
The following example demonstrates how to use the this keyword to access instance fields when constructor parameters have the same names.
Output:
The name is John The age is 20
Explanation:
In this example, we create a class named Student that contains two fields: name and age, and then we create a constructor which accepts two parameters. After that, we create the method ShowDetails() that displays the student's name and age. In the main() method, we create an object student1 and initialize it with values. After that, the ShowDetails() method is called and displays the students details.
Let us take an example to illustrate this keyword that is used to initialized class fields and the constructor.
Output:
101 Michael 890000 102 Stuart 59000
Explanation:
In this example, we create the class named Staff that contains three fields: StaffId, StaffName and StaffSalary, and then we create a constructor which accepts three parameters. After that, we create the method ShowInfo() that displays the StaffId's, Staff Name's, and Staff Salary's. In the main() method, we create object staff1 and staff2, and then initialize them with values. After that, the ShowInfo() method is called and displays the staff details.
There are several uses of this keyword in C#. Some of them are as follows.
In C#, several characteristics of this keyword are as follows:
There are several ways to use this keyword in C#. Some of them are as follows:
In C#, this keyword is used to resolve the naming conflicts when a method parameter has the same name as an object's member variable.
Let us take an example to demonstrate how we can access data members using this keyword in C#.
Output:
Value of number: 10
Explanation:
In this example, we create a Sample class that contains a private integer data member and two methods, AssignValue and Display. The AssignValue method sets the value of a number using the 'this' keyword. In the main method, we create an object and assign the values and then call the Display() method to show the result.
In the C# programming language, the technique of calling multiple methods in the same object in a single statement is known as method chaining. The this keyword is necessary to enable method chaining because it returns the current instance of the class.
Let us take an example to illustrate the method chaining in C#.
Output:
The value is 25
Explanation:
In this example, we create a Chain class that contains a private integer data member and two methods, SetValue() and Display(). After that, the SetValue() method uses the this keyword to return the current object, which allows method chaining. Finally, it allows us to call multiple methods on the same object in a single statement, which improves the code readability and fluency.
In C#, the this keyword is used to refer to the current instance of the class. It can be used to pass the current object as an argument to a method or constructor.
Let's see an example to illustrate how we can pass the current objects using this keyword in C#.
Output:
Inside Show method
Explanation:
In this example, we define the Sample class that contains three methods: Show(), Call(), and Execute(). The Show() method uses the Console.WriteLine() function to display the value. The Call() method takes an object of the class as a parameter and calls its Show() method. In the Execute() method, this method is used to pass the current object to the Call() method. After that, we create the object of the Sample class and call the execute function.
In C#, the this keyword is used inside a constructor to make reference to the class's current instance. It can be used to invoke a different constructor within the same class.
Let us take an example to solve the naming conflict in the constructor using this keyword in C#.
Output:
Constructor executed. Initial number: 10 Updated Number: 20
Explanation:
In this example, we create a Sample class that contains a private integer data member and two methods: UpdateNumber() and Show(). The Sample() constructor sets the initial value of the number using the this keyword to resolve a naming conflict. The UpdateNumber() method is used to update the value of the number, and the Show() method is used to display the updated value of the number. In the Main() method, we create an object, set its value, and display it using method chaining.
We request you to subscribe our newsletter for upcoming updates.