this.
''this." what is this command used for?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Munesh SharmaPosted Jun 25, 2015, 2:16 AM
Nilesh JadavPosted Jun 25, 2015, 2:12 AM
The keyword "this" in C# is used to access the field variables. All the field variables must be accessed using "this" keyword.
It is useful when the field variable and the local variables of a method have same name. In the above case local variables overrides field variables and field variables are no more visible inside the method. So this can be used to refer field variables to make visible inside method.
output:
a=2 b=10
It is the class variable
Now the local variables are:
a=23 b=34
When we write this.a and this.b, it will refer to the field variables. But when we write only a and b it refers to the local variables of the method Get().
As the field variable and local variables have the same name we have to use the this keyword. We can't access a field variable without this keyword in C#. this always refer to the members of same class.
Hope this works !
Upendra Pratap ShahiPosted Jun 25, 2015, 12:56 AM
For example, let's assume you have the following class:
1.Reference to current instance of class
2.Call another constructor from a constructor in same class.
3.Declare indexer
Below example covers above 3
Rajeesh MenothPosted Jun 24, 2015, 11:57 PM
Bhushan BhasmePosted Jun 24, 2015, 11:21 PM
Pankaj Kumar ChoudharyPosted Jun 24, 2015, 10:27 PM
Debendra DashPosted Jun 24, 2015, 10:50 AM
MarcPosted Jun 24, 2015, 9:43 AM