9

In the following constructor, is the 'this' keyword required? I know I can remove it, it complies and everything is okay. If I omit 'this' will that cause problems for me down the road? Is ommission of 'this' considered bad practice?

    // Constructor:
    public Employee(string name, string alias)
    {
        // Use this to qualify the fields, name and alias:
        this.name = name;
        this.alias = alias;
    }
DenaliHardtail
  • 27,362
  • 56
  • 154
  • 233

6 Answers6

26

No, this is purely optional in almost all cases. The only reason it is required in your example is to disambiguate between local variables and parameters and member variables that happened to have the same identifier name.

You could avoid the use of this by renaming either the parameter or the field to something unique.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 1
    So it IS NEEDED. if he doesn't use this, then the code won't work correctly. Will it even compile without "this"? – Alex Baranosky Oct 05 '09 at 02:24
  • 1
    It will compile but with warnings. – Andrew Hare Oct 05 '09 at 02:38
  • The reason it is required is that the compiler would assume that `name` meant the parameter, not the field in all cases where `this` was omitted. Obviously it would compile but there would be logical errors in the program. – Andrew Hare Oct 05 '09 at 02:39
  • "this" can also differentiate with member methods if the class implements a base class (base vs. this). – jro Oct 05 '09 at 02:48
10

Yes, you need it in your code example. C# will always assume you mean local variable, so if a member variable and a local variable exist with the same name, you must use this, otherwise it will be assumed that you are referring to the local variable.

In your code example, if you neglect the this, you are effectively assigning name (or alias)'s value to itself (thus accomplishing nothing).

Smashery
  • 57,848
  • 30
  • 97
  • 128
5

in the case below this is absolutely required. But you can get around it by choosing variable names carefully. In my humble opinion, there is nothing wrong with the example below and I do not mind having to use this. In fact, I often use this even when it is not required just to prompt intellisense and auto-complete in my IDE.

internal class Something
{
    private string name;
    public Something(string name)
    {
        this.name = name;
    }
}
J.Hendrix
  • 2,199
  • 4
  • 20
  • 26
2

It's optional, unless you have a member variable named 'name', in that case, your line there will be assigning to itself.

I always like to access all member variables with "this", just to be explicit. Some people like to use "_" as a prefix, or even "m".

Noon Silk
  • 54,084
  • 6
  • 88
  • 105
  • I've been using the "_" but lately I'm finding a fair amount of sample code, like that shown above, that uses the 'this' keyword, hence the question. – DenaliHardtail Oct 05 '09 at 01:15
  • 1
    I prefer the 'this' since it can't be a mistake; someone may accidentally name a local variable "_", but with `this`, it's always obvious. – Noon Silk Oct 05 '09 at 01:17
  • @sikly: proper tools can prevent accidental local variables with leading "`_`". So can human eyes. – John Saunders Oct 05 '09 at 01:30
  • 3
    I prefer to use the most basic of all tools (the compiler) to find my errors. – Noon Silk Oct 05 '09 at 01:41
  • @Scott -- that is likely to be a result of people starting to use the StyleCop tool on default settings to automate coding standards. – Steve Gilham Oct 05 '09 at 06:51
0

in your example, if you don't use "this" something worse will happen: "Variable Hiding"

Bohn
  • 26,091
  • 61
  • 167
  • 254
0

In C# it is most common to either name the private variable _Name or m_Name. This also makes it easy to visually match up Properties because if you have a property Name, which uses a local private variable _Name, it is easy to see quickly what it is for.

Without the this keyword in your example, all you are doing is assigning the local variable to the local variable (and not to the private variable). Which is why it compiles, but won't do what you think it is doing.

mike
  • 3,146
  • 5
  • 32
  • 46
  • 3
    Actually, the use of sigils like _ or m_ is against the MSFT coding guidelines as embedded in the FxCop and StyleCop tools; the MSFT coding standard is to require the "this." prefix, even if not actually necessary to distingish a field from a local vaRIABLE. – Steve Gilham Oct 05 '09 at 06:50
  • @SteveGilham is right, don't use m_ prefixes in your C# code – Jon Jul 22 '15 at 18:17