Click here to Skip to main content
15,887,962 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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 me to get out from this silly confusion.

Regards
Atul Khanduri
Posted
Comments
ridoy 12-Oct-13 4:55am    
posting same question twice doesn't make any sense!
Atul Khanduri 12-Oct-13 5:23am    
Oh sorry sir...
But that was due to my slow internet access...
ridoy 12-Oct-13 7:29am    
It's ok.

1 solution

this doesn't exactly qualify members hidden by a similar name - although that is what it is used for in most cases - it is a reference to the current instance of a class.

So if you have an Employee class as in your example:
C#
public class Employee
   {
   private string name;
   private string alias;
   public Employee(string name, string alias)
      {
      this.name = name;
      this.alias = alias;
      }
   }
Then the constructor needs to access the class instance members via this in order not to be talking about the local, parameter based, versions which will have precedence. If you didn't have such a mechanism, then you wouldn't be able to access the class level version inside that constructor.

It's like if you are talking to your mate "Joe" and you need to refer to a different "Joe": "Joe at work", for example.

Normally, you would just call him "Joe", but because there is a local person also called "Joe" that would be confusing so you absolutely specify who you are talking about by adding the "at work" suffix.
 
Share this answer
 
Comments
ridoy 12-Oct-13 4:53am    
5ed.
Atul Khanduri 12-Oct-13 5:28am    
Thanks for the explanation sir....:)
It is simple but i was confused and now it is clear.... :)
OriginalGriff 12-Oct-13 5:37am    
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