1

I understand that keyword "this" refers to the variables outside constructor within a class variables. but why not just change name of parametor to (string _name, string _alias) , all the example of "this" in tutorials just states the obvious, but i'd like to know the working use of why this keyword is needed in a certain situation.

private string name;
private string alias;
   public Employee(string name, string alias) 
    {
       this.name = name;
       this.alias = alias;
    }
Diggie
  • 142
  • 3
  • 10

2 Answers2

3

In your example, you could put an underscore on your field names so this is not needed (it would still be allowed, but not needed, as it can be inferred if there is no clash with a local variable). In fact, Microsoft's standards encourage doing so.

However, there are still plenty of cases where you may need the this keyword.

One example is if you had a utility in another class which accepted an Employee, such as:

public static class Utility
{
    public static void SomeMethod(Employee employee)
    {
    }
}

and you wanted to call this utility from within the Employee class, you would have to call it as Utility.SomeMethod(this);.

Also, extension methods called on the current instance must be prefaced by this.. Given the following extension method:

public static class UtilityExtensionMethods
{
    public static void SomeExtensionMethod(this Employee employee)
    {
    }
}

if you wanted to call this extension method from within the Employee class, you would have to call it as this.SomeExtensionMethod();.

jam40jeff
  • 2,576
  • 16
  • 14
1

I name private member variables _x to avoid such cases1. Arguments for/against this abound.

private string _name;
public Employee(string name, string alias) 
{       
   // Equivalent to `this._name = name`, as _name is not shadowed
   _name = name;
}

On the other hand, I am opposed to naming parameters _x because parameter names are part of the public contract2. This contract should be clean, stable, and should not be dictated by the internal implementation which includes member variable names.

In the past I used names like employeeName, aName, and theAlias for parameters in such cases, but I disliked the artificial modifiers that ended up being used. If I could not use underscores for private variables I would use this.x and not think twice about it.


1 Requiring this form is due to this being "implicit" in C# and variable shadowing. That is, this.x is needed because x will refer to a local variable/parameter, should it exist - and it does here.

private string name;
public Employee(string name, string alias) 
{
   // Re-assignes parameter to itself.
   // Does NOT assign member variable!
   name = name;
}

2 I believe that underscore in names break "CLS-compatibility", but that is not a concern for me, and only using this convention for private member variables avoids exposing such incompatible names publicly.

Community
  • 1
  • 1
  • Acconding the guidelines of from Microsoft they are agains prefixing the fields.. `Do not apply a prefix to field names or static field names. Specifically, do not apply a prefix to a field name to distinguish between static and nonstatic fields. For example, applying a g_ or s_ prefix is incorrect.` http://msdn.microsoft.com/en-us/library/ta31s3bc%28VS.71%29.aspx, but many users (http://stackoverflow.com/questions/1630844/c-sharp-member-variables-fields-naming-convention) prefix.. I am against prefix.. ;-) – TryingToImprove Feb 09 '13 at 19:38
  • 1
    @TryingToImprove I would not use `g_` or `s_`. That is clearly wrong (to me), especially when to denote between static/non-static. `m_` is just too noisy, but I find `_` for *private member variables* just right. I actually added the [excerpts to this answer](http://stackoverflow.com/a/1630928/166390), and follow [the linked article guidelines](http://10rem.net/articles/net-naming-conventions-and-programming-standards---best-practices) :D –  Feb 09 '13 at 19:40
  • 1
    I dont argue with you :-) I can see the pro/cons for doing so. I prefix "privates" in JavaScript with `_`. But in C# my privates are camelCase and public are CamelCase. I can't remember last time I had to have a "public" field. – TryingToImprove Feb 09 '13 at 19:44