C# 命名指南
我有这样的课程:
class DoSomething
{
private int timesDone;
...
}
命名变量“timesDone”的正确方法是什么?
有时我看到命名为m_timesDone。这是正确的吗?在哪里可以找到有关命名指南的信息?
谢谢你!
I have this class:
class DoSomething
{
private int timesDone;
...
}
Which is the right way to named variable 'timesDone'?
Sometimes I see named as m_timesDone. Is this correct? Where I can find information about naming guidelines?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
没有普遍适用的正确方法。选择您喜欢的命名约定并坚持使用。
There is no universal right way. Chose a naming convention of your liking and stick with it.
根据 MS 标准,您的代码是可以的。当您拥有高级 IDE 时,并不需要使用 m_ 作为前缀。但是,可以使用像 _ 这样的短前缀来利用自动完成功能来快速排序班级成员。
我建议您获取一份“框架设计指南:约定、习惯用法和模式”的副本用于可重用.NET 库”一书,了解有关 MS 标准的更多信息
According to MS standards your code is OK. Having prefixes as m_ is not really necessary when you have advanced IDE. However short prefix like _ can be used to take advantage of auto complete feature to quickly sort out class members.
I would recommend you to get a copy of "Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries" book to learn more on MS standards
绝对不要使用 m_timesDone。
简单地说“private int timesDone”。
您可以通过阅读一些好书(例如 Code Complete)来了解如何命名变量。
Definitely do not use m_timesDone.
Simply put "private int timesDone".
You can learn about how to name variables by reading some good books such as Code Complete.
很多人都会像你那样做。然后,您可以将其引用为“
但是,我不喜欢这样”,因为我不喜欢键入“this”以避免与方法参数名称发生冲突。只要它可读且一致就可以了。
Many people do as you have it there. You would then reference it as
However, I don't like this because I am not a fan of typing 'this' to avoid clashes with method parameter names. As long as it is readable and consistent you will be fine.
用 m_ 作为成员字段前缀的惯例来自 C++ 的早期,当时 匈牙利表示法受欢迎的。它不是 C# 约定,并且由于大多数 C# 代码是使用最新的 Visual Studio 编写的,因此它增加了视觉噪音,但没有任何相应的优势,因为无论如何您都可以轻松查看变量的范围。不要使用 m_。
匈牙利表示法在 C# 中的唯一示例是在接口类名前面加上 I 的做法,例如 IDisposable。
The convention of prefacing member fields with m_ comes from the early days of C++, when Hungarian notation was popular. It is not a C# convention, and since most C# code is written using a recent Visual Studio it adds visual noise without any corresponding advantage, because you can easily see the scope of a variable anyway. Do not use m_.
The lone example of Hungarian notation that has found its way into C# is the practice of prefacing interface class names with I, such as IDisposable.
您可以直接在 MSDN 站点上找到一些信息:
http://msdn.microsoft.com/en-us/library/ms229002。 ASPX
You can find some information directly on the MSDN site:
http://msdn.microsoft.com/en-us/library/ms229002.aspx
您会发现的唯一一致点是它应该不是 TimesDown,而是应该以小写字母开头。
在较旧的出版物(MS、MSDN)中,不鼓励使用前导下划线。后来它又回来了,特别是对于属性的支持字段:_timesDown。
The only point of agreement that you will find is that it should not be TimesDown, but that it should start with a lower case letter.
In older publications (MS, MSDN), the use of a leading underscore is discouraged. Later it is back, especially for backingfields for properties: _timesDown.
通常采用的策略是:
对于Class& 方法:帕斯卡命名法
对于
例如
变量:骆驼命名法< /strong> 例如 timesDown
局部变量:
全局变量:
我希望这可以帮助你:)
The Strategy usually adopted is :
For Class & Method: Pascal Casing
e.g.
e.g
For Variables: Camel Casing e.g. timesDown
Local Variables:
Global Variable:
I hope this may help you :)