简体   繁体   中英

In C#, what is the generally accepted way to refer to member properties within the class?

Have read through the MSDN naming guidelines and could not find a clear answer, other than that you should try to avoid underscores in general. Let's say I have the following:

public class Employee
{
    private string m_name;  //to store property value called Name

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

    public void ConvertNameToUpper()
    {
        //by convention should you use this
        return m_name.ToUpper();

        //or this
        return Name.ToUpper(); 
    }
}

What is the proper naming convention for m_name in the above? For example, in code I inherit I commonly see:

  • m_name
  • _name
  • name
  • myName or some other random identifier

Which one (or another) is most commonly accepted?

As a follow-up, in the methods of the class, do you refer to the internal (private) identifier or to the public property accessor?

I think that, whatever naming convention you use, the most important thing is that you stay consistent. I mean, if you choose to name private members like _name , then always do it like this instead of once using _name, and the other time m_name. I personally use the underscore-prefix convention. (one of the reasons is because I use NHibernate, and NHibernate has a 'field.camelcase-underscore' access strategy.

For your other question: It depends on what you want to do.
Does your property contain extra logic, and do you want this logic to be executed when you refer it ? Then use the property. You don't want to execute the logic ? Use the field. Eric Lippert has written a post regarding this on his weblog.

For your folluw-up: it all depends on the situation. If your property contains some additional logic, and you don't want to execute that additional logic when accessed from within the class, then use the backing field ...

First of all - for the simple get/set cases I would recommend that you use automatically implemented properties. If you do that the compiler will generate the underlying variable, and you only reference the property itself.

Other than that, I suggest you pick one of the above or anything similar and just stick to that. The company I work for uses vName where the "v" indicates that this is the value of the property.

I use Style Cop which enforces some styles on your code. I find this very useful and all my team members also use this.

While there are great discussions around the use of Style Cop, one thing I would suggest is that if you use Style Cop that is to leave all styles enabled. This way when you share between users it makes things a lot easier.

One of the things this inforces is that you can not name your private fields with underscores. So I generally use camelCase when writing private fields and then PascalCase for public Properties:

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

Style Cop also enforces the use of this. which makes things a lot easier to read.

The most common one I've seen in example code is a simple _ prefix.

However, what really matters is that the team agrees what the standard is and sticks to it.

If you can't use an automatically implemented property as Brian Rasmussen suggest and you have to have a private member, then I would recommned the underscore prefix, _name.

In intellisense, it's not immediately obvious whether an item is a parameter, local or private member as they all have the same symbol (blue cube). If, however, you move cursor to a particular item then the tooltip tells you which of these it is.

I find the underscore prefix is a handy visual aid which makes it immediately obvious that it is a private member without having to move the cursor.

我曾经非常反对'_'前缀,但是当你想快速访问一个成员字段而不必输入很多字母时,它对intellisense非常有用。

一方面我同意你应该使用公司标准,另一方面我会努力遵守行业标准。

I would echo previous answers in that you should stick with a scheme that your team uses, or if you are not in a team, be consistent with whatever you do use.

Personally, I use the the underscore prefix as I find it gives me a good visual cue.

I think the generally accepted naming convention is solely to make the name meaningful (and the code clean and simple). In my opinion, if the identifiers in my code need visual cues for any reason, it's too complex and the names are usually not entirely accurate.

I've worked on code that required a manual just to read... "m" meant class-level, "p" meant parameter, etc. The naming convention was developed to make the code easier to read but it ended up doing just the opposite because developers ran with the implication that "good" naming conventions meant readable code.

Just make sure this Dennis Green (former Arizona Cardinal coach) quote applies to your identifiers: "They are who we thought they were!"

I try to only access the member variable when I have to, such as when the property is read-only or I want to bypass any setting logic.

One reason for this is that if you do add logic to the setter later, you will most likely want it to be used everywhere.

For class-level variables, our coding standards say use mVariableName or m_VariableName. The main thing is to follow your company/teachers/etc. coding standards/practices.

I, personaly, only access the variable through its getter/setter if it has one. Even if the variable is only used internally in the class, I use the automatic properties. This way, I add a layer of abstaction wich means less code to refactor if I changed something.

BTW, your void function can't return a string..... :-)

我只是想补充一点,MSDN命名指南没有指定这个,因为它只有公共接口的指南(即属性名,公共方法,公共方法参数等等)。他们不关心私有成员风格,就微软而言,你可以使用你和你的团队想要的任何东西。

First of all, I, and many others I have worked with have done away with the use of prefixing private members with "m_". Next, whenever I refer to the private member within the class, I usually use the word this as in "this.privateMemberVariableName". Using this is enough to distinguish that the variable is not a local variable or a variable passed as a parameter within the method.

I do refer to the public property name if it contains logic that is other than just referring to the private member variable, such instancing a connection or saving the property value in a view state.

框架设计指南书说你不应该在变量前面添加_ - 你应该只使用一个小写的变量名称,而Code Complete第二版我相信你不应该在你的变量前加上m_。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM