Guys i just started to learn C# and i have no idea about “this” reference in c# someone can give me a very simple example in real life. I mean why do i need to use “this” in C# or where can i use “this” what is the benefit?
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The this keyword eliminates the need to have unique variable names for constructors.
Oftentimes, you already named a variable well and to have a unique name for the constructor, you would either have to come up with a new name or otherwise mar the good one you have.
In addition, it allows you to name local variables and instance variables the same and have a way to use the one you wish.
For example:
In C++ ‘this’ is a pointer, so you would need to reference members using the ‘->’ operator.