Search This Blog

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 -

1. this keyword is used for differentiating instance members from input parameters of a method.

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.

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).

Comments

Post a Comment

Popular posts from this blog

Great android apps require great design. For great design you need to master layouts. Layout is one of the basic building block of Android User Interface. Lets jump right in and try designing layout for our app - Just Maths.

Just Maths has 3 screens - Main Menu Screen, Game Screen and Score Screen. Each of the screen is designed using Relative Layout and Linear Layout. There are other layouts as well like Frame Layout, Table Layout and Grid Layout. But you can design anything using only Relative Layout and Linear Layout. These layouts are nothing but View Groups.

Want to build Android Apps really fast?
With Android Studio and a blend of Java and XML, you will be up and running and coding Great Android Apps in no time.
Let's get started. We will be creating a very simple app named Just Maths that will ask user a Maths question. User either presses (✔) or (X) to answer the question. This is going to be a project based learning wherein you will have something concrete in the end.

Here's is a quick way of implementing Auto-Complete in your webpage. Auto-Complete dropdowns are some textbox type looking elements on webpage. When you type on these textboxes you will be prompted with value options just like in dropdowns. The good part of using this method is you need not to using any jQuery for this. The bad part is this will only work in modern browsers which support HTML5.