简体   繁体   English

在非公共字段上使用前缀

[英]Using a Prefix on Non-Public Fields

I don't know anyone who uses a 'm_' to prefix non-public fields (myself included), but I am aware of three reasons why it should be done: 我不知道有人使用'm_'作为非公共字段(包括我自己)的前缀,但是我知道应该这样做的三个原因:

  1. The CLR specification insists that property names and corresponding fields not differ merely by letter casing. CLR规范坚持认为,属性名称和相应的字段不应仅因字母大小写而不同。 Some languages are case-insensitive. 某些语言不区分大小写。 _ ” could be used, but “ m_ ” is preferred because names should start with alphabetic characters. 可以使用“ _ ”,但首选“ m_ ”,因为名称应以字母字符开头。 (This comes from coding standards suggested by Dan Rigsby) (这来自Dan Rigsby建议的编码标准
  2. A mere “ _ ” prefix is not CLI compliant, whereas “ m_ ” is CLI compliant. 仅“ _ ”前缀不符合CLI,而“ m_ ”则符合CLI。
  3. Intellisense, at least in WPF as of VS 2008, does not distinguish between types defined in XAML and types defined in code-behind. 至少在VS 2008及更高版本中,Intellisense不会区分XAML中定义的类型和Xcode中定义的类型。 A 'm_' used in the code behind would resolve the issue. 后面的代码中使用的'm_'将解决此问题。

My question is this: would you (yes, you!) be able to accept this as a new standard on projects you are currently working on, given the reasons stated, or would you still just lose your mind in unbridled hatred of the idea? 我的问题是:您是否(是的,您!)能够将其作为当前正在研究的项目的新标准,考虑到上述原因,还是您会因为对这个想法的不满仇恨而失去理智?

ps Please don't tell me "Hungarian" is bad, since this suggestion - in isolation - really has no connection with Hungarian. ps:请不要告诉我“匈牙利语”是不好的,因为这个建议(孤立地讲)确实与匈牙利语没有任何关系。

Most people I know (including myself) just use _ as a prefix for non public fields. 我认识的大多数人(包括我自己)仅将_用作非公共字段的前缀。 It's shorter to type, and it makes the fields appear all together in intellisense. 它键入的时间较短,并且使字段在智能感知中一起出现。 I also find it more readable than m_ 我还发现它比m_更可读

This question has been asked on SO many times, but once again, it comes down to personal preference and, as long as you are consistent, you're not going to have any problems with either approach. 这个问题已经问过很多次了,但是又一次取决于个人喜好,只要您始终如一,那么这两种方法都不会有任何问题。

As far as I know, most C# coding standards call for lower case member variable names with no prefix. 据我所知,大多数C#编码标准都要求使用无前缀的小写成员变量名称。 Personally, I don't like this approach because your member variables tend to clash with method parameters, so you constantly have to qualify them with 'this'. 就我个人而言,我不喜欢这种方法,因为您的成员变量倾向于与方法参数冲突,因此您经常需要使用“ this”对其进行限定。

I use a single underscore before the name of member variables, but I am sure there are many people out there who don't like that either. 我在成员变量的名称前使用了一个下划线,但是我敢肯定有很多人也不喜欢。 It boils down to being consistent throughout the code base. 归结为整个代码库的一致性。

The CLS not CLR specification insists that public and protected members not differ only by case. CLS不是CLR规范坚持认为, 公共成员和受保护成员并非仅因情况而异。 It's important to be aware of the fact that there is no need to make your types CLS-compliant unless you plan on using them in other projects that span multiple languages. 重要的是要意识到以下事实:除非您打算在跨多种语言的其他项目中使用它们,否则不必使您的类型符合CLS。 Even then, if this is an internal project there may not be a need to follow all of the CLS guidelines. 即使这样,如果这是一个内部项目,也可能无需遵循所有 CLS指南。

In any case, this applies only to members that are potentially visible outside of the declaring assembly (in other words, private and internal members are exempt), this suggestion is not relevant to non-public fields. 无论如何,这仅适用于在声明程序集外部可能可见的成员(换句话说, private成员和internal成员是免税的),此建议与非公共字段无关。

That being said, the concept of naming conventions has been debated since we were able to name variables. 话虽如此,自从我们能够命名变量以来,命名约定的概念一直在争论。 Personally, no, I do not like the idea of prefixing variables with m_ , but that's a personal preference. 就个人而言,不,我不喜欢在变量前加上m_的想法,但这是个人喜好。

Yes. 是。 It is important that all programmers accept the standards of the projects they are working on - it is a good sign that management is insisting on standards, whether the individual agrees with them or not. 所有程序员都必须接受他们正在从事的项目的标准,这一点很重要-这是管理层坚持标准的好兆头,无论个人是否同意。

Personally, I think prefixing with "m_" is a waste of a keystroke, but if it's in the standards then that's what I'll adhere to. 就我个人而言,我认为以“ m_”作为前缀是浪费按键,但是如果它在标准中,那我将坚持。

If I was writing the standards, I would specify that "_" should be used as a prefix to non-public fields. 如果正在编写标准,我将指定“ _”用作非公共字段的前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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