views:

97

answers:

4

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.

+1  A: 

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
A: 

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
A: 

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
A: 

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

Garry Shutler