1

I have seen several naming conventions used for fields in C#. They are:

Underscore

public class Foo
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
}

This

public class Foo
{
    private string name;
    public string Name
    {
        get { return this.name; }
        set { this.name = value; }
    }
}

Member Prefix

public class Foo
{
    private string m_name;
    public string Name
    {
        get { return m_name; }
        set { m_name = value; }
    }
}

Which do you prefer? Is there a different way you prefer to do it? Just curious to know what others feel is a best practice.

Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
Brian Genisio
  • 47,787
  • 16
  • 124
  • 167
  • Dup with many, including: http://stackoverflow.com/questions/450238/to-underscore-or-to-not-to-underscore-that-is-the-question, http://stackoverflow.com/questions/111605/what-kind-of-prefix-do-you-use-for-member-variables, etc – Marc Gravell May 12 '09 at 12:16
  • and http://stackoverflow.com/questions/833811/underscore-prefix-on-member-variables-intellisense (many, many, more...) – Marc Gravell May 12 '09 at 12:17
  • Agreed. Somehow, I didn't find them when searching. Also voting to close. – Brian Genisio May 12 '09 at 12:19
  • http://stackoverflow.com/questions/762025/ – Greg D May 12 '09 at 12:21

4 Answers4

1

I usually use:

public class Foo
{
    private string name;
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
}

just removed the this. since it's rather redundant

Fredrik Leijon
  • 2,792
  • 18
  • 20
0

The best practice is to pick one, and be consistent.

I prefer "", personally. "m" has always seemed to me to make the "memberness" seem to be something special, as though it were for the benefit of developers coming from a background where no "members" exist. Sort of the same with "this.". I'd prefer just "name" instead.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
0

1) This is a purely stylistic/subjective question, so one of these is, in general, as good as another. (I qualify that because the last time I answered a question like this, even with a nearly identical qualification, I had all kinds of people voting me down and telling me I was wrong.)

2) I use the this method. It's the default technique used by StyleCop, and I don't have any other source analysis tool available to me at the moment. It's perfectly acceptable.

Greg D
  • 43,259
  • 14
  • 84
  • 117
0

I always use what you call the this style, though I won't religiously use this.

Garry Shutler
  • 32,260
  • 12
  • 84
  • 119