Click here to Skip to main content
15,887,344 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,
This may be a silly question but i am curious to know this.

I know "this" keyword is used to qualify members hidden by similar name. But now my question is what type of hidden members??

I saw the following code:

C#
public Employee(string name, string alias)
{
      this.name = name;
      this.alias = alias;
}


But still i am unable to get the phrase "qualify hidden members".

So, help to get out from this silly confusion.

Regards
Atul Khanduri
Posted

Let me try and explain this "qualify" piece.

Lets say you have the same variable name in two places one at class and one at constructor / method level.
Using the example in the article itself -

C#
public class Employee
{
private string name = string.empty;
private string alias = string.empty;

 public Employee(string name, string alias)

    // Use this to qualify the fields, name and alias:
    this.name = name;
    this.alias = alias;
 }
}


There are two scoped name and alias variables - one at class and another at constructor level.
When we say this, we qualify the variable to be the class level one.

this.name = name;

The value of the class level variable has been set to the value passed in the scope of the constructor.
 
Share this answer
 
Comments
Atul Khanduri 12-Oct-13 5:37am    
Thanks for the explanation.... :)
this keyword is meant to qualify the current instance of the class.
I don't know where you got this "qualify hidden members" thing.
this (C# Reference)[^]
 
Share this answer
 
Comments
Atul Khanduri 12-Oct-13 4:31am    
I already read that article in the link you referred.
But there also the same thing is written:
"To qualify members hidden by similar names".
I just want to know what type of "hidden members" it is??
phil.o 12-Oct-13 4:35am    
OK, in this context:
there are two fields, named 'name' and 'alias' ; there are also two parameters of the constructor with the same names ('name' and 'alias'). 'this' keyword is used to differentiate class fields from constructor parameters.
Atul Khanduri 12-Oct-13 5:19am    
Okkk...
Got it....
Its simple but i was confused...
Thanks... :)
phil.o 12-Oct-13 10:08am    
You're welcome ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900