Choosing naming conventions for class fields in C #?

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

Underline

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

      

it

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; }
    }
}

      

What do you prefer? Is there any other way that you would rather do? Just curious to know what others think is best practice.

+1


a source to share


4 answers


I usually use:

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

      



just removed it. as it is rather redundant

+1


a source


The best practice is to pick one and be consistent.



I prefer "personally. I have always felt that" membership "seems to be something special, as if it was for the benefit of developers coming from a background where there are no" members ". Likewise. I would rather just" name ".

0


a source


1) This is a purely stylistic / subjective question, so one tends to be as good as the other. (I believe that since the last time I answered such a question, even with almost the same qualifications, I had different people who voted for me and said that I was wrong.)

2) I am using the method this

. This is the default technology used by StyleCop and I have no other source code analysis tool available to me at the moment. This is perfectly acceptable.

0


a source


I always use what you call this style , although I won't religiously usethis.

0


a source







All Articles