this Keyword in C#
In our previous post, we learned about static and instance members of a class. Instance of a class is same as object. this keyword refers to the current instance. To understand "this", let us take some examples where this keyword is used -
In above code snippet - radius is passed as input in our parameterized constructor. This parameter hides the instance variable as they both have a common name radius. To assign input value to our instance variable radius - we use this.radius. Here this keyword is used to avoid the ambiguity. So, when we say - this.radius - we actually mean radius property of current instance. In simple words, Line 9 means, assign input parameter radius to the radius property of current instance.
In above code snippet, default constructor calls the parameterized constructor with a value of 5. So whenever somebody uses default constructor to create a new instance, new Circle object is created using the parameterized constructor. Default constructor calls the parameterized constructor using this keyword. i.e. calls parameterized constructor of current instance.
In above code snippet, to pass the current circle object to drawCircle method of Shape class, we use this keyword. In simple words, to pass current object itself to other methods as parameter, we use this keyword.
There are other uses of this keyword in Indexers and Extension methods. These topics will be covered in future posts. this keyword seems confusing in the beginning but once you understand, it is a very simple concept. Just remember - this refers to the current instance. Let us know if you have any concerns or any query. Happy Reading (refer full series here - C# Tutorial Index).
1. this keyword is used for differentiating instance members from input parameters of a method.
class Circle { int radius; public Circle(){} public Circle(int radius) { this.radius = radius; } }
In above code snippet - radius is passed as input in our parameterized constructor. This parameter hides the instance variable as they both have a common name radius. To assign input value to our instance variable radius - we use this.radius. Here this keyword is used to avoid the ambiguity. So, when we say - this.radius - we actually mean radius property of current instance. In simple words, Line 9 means, assign input parameter radius to the radius property of current instance.
2. this keyword is used to call one constructor from another constructor.
class Circle { int radius; public Circle() : this (5) {} //Default Constructor calls parameterized constructor public Circle(int radius) { this.radius = radius; } }
In above code snippet, default constructor calls the parameterized constructor with a value of 5. So whenever somebody uses default constructor to create a new instance, new Circle object is created using the parameterized constructor. Default constructor calls the parameterized constructor using this keyword. i.e. calls parameterized constructor of current instance.
3. this keyword is used to pass current instance to other methods.
class Shape { public void drawCircle(Circle objCircle) { //this method accepts Circle object to draw it on the screen. } } class Circle { //... void paintShape() { Shape shape = new Shape(); shape.drawCircle(this); } }
In above code snippet, to pass the current circle object to drawCircle method of Shape class, we use this keyword. In simple words, to pass current object itself to other methods as parameter, we use this keyword.
There are other uses of this keyword in Indexers and Extension methods. These topics will be covered in future posts. this keyword seems confusing in the beginning but once you understand, it is a very simple concept. Just remember - this refers to the current instance. Let us know if you have any concerns or any query. Happy Reading (refer full series here - C# Tutorial Index).
Comments
Post a Comment
Hey there, liked our post. Let us know.
Please don't put promotional links. It doesn't look nice :)