2

Possible Duplicates:
When do you use the “this” keyword?
Best practices for using the ‘this’ keyword in C#

I have a style question regarding the this keyword. Do you use this when self-referencing auto-implemented properties or methods within a class for the sake of clarity?

For one example, in your Constructor, do you write your parameter assignments as:

public class Foo
{
    public string FooProperty { get; set; }
    public Foo(string fooProperty)
    {
        this.FooProperty = fooProperty;
    }
    ...
}

OR as:

public class Foo
{
    public string FooProperty { get; set; }
    public Foo(string fooProperty)
    {
        FooProperty = fooProperty;
    }
    ...
}
Community
  • 1
  • 1
Troy DeMonbreun
  • 3,860
  • 3
  • 25
  • 35

1 Answers1

7

IMHO, the this is a very useful keyword. Once I see the "this", I know it is a class variable. Otherwise, I would have to check whether its a parameter, a variable declared within the method. "This" saves time :) (ambiguous joke hehe)

Samuel Carrijo
  • 17,449
  • 12
  • 49
  • 59
  • 2
    +1 One of the things I wonder is why many people and even some code analysis tools recommend not to use "this". – Rui Craveiro Jun 26 '09 at 18:17
  • Yeah, I've come to think is is sensible practice, too. I'm in the habit of using the keyword on all class variables and properties (not methods of course!), since it's somewhat more elegant than the underscore prefix. – Noldorin Jun 26 '09 at 18:19
  • It's easier to just name member variables using a prefix like "m_" for instance. – Zack Jun 26 '09 at 18:37
  • 1
    Using "m_" has some issues. It makes code a little more dirty, and it requires who reads the code to know this convention (higher learning curve). Anyone who sees "this" will immediately know what that means (hopefully) – Samuel Carrijo Jun 26 '09 at 18:45
  • This is why I use IDE's that highlight properties for me :P Call me a noob but I'd rather have clean code and an advanced working environment then complicated naming conventions and hard-on-the-eyes coloring. – Qix - MONICA WAS MISTREATED May 23 '12 at 04:08