几个C#命名约定问题
1)
声明变量的策略是什么? 您应该始终使用关键字private
,还是可以跳过它?
string MyVar1;
我看到的
private string MyVar1;
唯一原因是 Microsoft 有一天可以将默认访问修饰符更改为 public
而不是 private
。
哪里说 private 是可选的? 是否引用了 MSDN?
2) 常量的命名策略?
我在编写常量时总是使用大写字母,但一位朋友告诉我,这违反了微软的命名政策,是吗?
const string MYVAR1;
对比
const string myVar1;
3) 帕斯卡还是骆驼?
就我个人而言,我认为骆驼只是看起来很丑。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
1)
private
关键字是可选的。 我非常怀疑微软是否会改变字段的默认可见性,因为这将是一个巨大的、突破性的改变(更不用说一个愚蠢的改变了)。 省略private
关键字只是个人喜好问题。2) 不要使用“大喊大叫”的常量 - 遵循框架的约定并使用 pascal 大小写(例如
ThisIsAConstant
优于 THIS_IS_A_CONSTANT)。1) The
private
keyword is optional. I highly doubt that Microsoft will ever change the default visibility of fields as this would be a massive, breaking change (not to mention a stupid one). Omitting theprivate
keyword is merely a matter of personal taste.2) Don't use "shouty" constants - follow the convention of the framework and use pascal casing (e.g.
ThisIsAConstant
is preferable over THIS_IS_A_CONSTANT).您可能对 Microsoft 的类库开发人员设计指南< /a>
You might be interested in Microsoft's Design Guidelines For Class Library Developers
不直接回答您的问题,但也许您会对 Microsoft 的 StyleCop 感兴趣。 这是一个用于分析源代码的样式和一致性规则的工具。 默认情况下,它采用 Microsoft 的样式指南。
Not answering your question directly, but maybe you would be interested in Microsoft's StyleCop. This is a tool for analysing your source code with respect to style and consistency rules. By default, it imposes Microsoft's styling guidelines.
根据 MS 指南命名常量的官方建议是:
The official recommendation for naming consts according to the MS guidelines, since no-one has actually specified it fully yet is:
private 是可选的,但需要额外输入。 我喜欢跳过它。
至于常数,这取决于您的喜好以及与您一起工作的人。 但如有疑问,请查看 .NET Framework 以及它们如何命名常量。
private is optional, but extra typing. I like to skip it.
As for the constancts, It depends on your preferences and who you're working with. But when in doubt, look at the .NET Framework and how they name constants.
我怀疑 Microsoft 是否会更改 C# 成员变量的默认行为。 我会将您想要公开的事物声明为私有,并明确声明您希望公开的事物为公开,只是为了清楚起见(如果没有别的事情)。
我认为常量的重要规则是仅使用每个人都同意并且您认为是常量的命名约定。 如果每个人都喜欢全部大写,那么就使用它。 如果您想更标准,请使用 Pascal 大小写。
I doubt Microsoft is ever going to change the default behavior for C# member variables. I would declare things private you want to be private and explicitly declare things public that you want to be public just for clarity if nothing else.
I think the important rule for constants is to just use a naming convention everyone agrees on and that you recognize as a constant. If everyone likes all upper case then use that. If you want to be more standard though use Pascal casing.
private 是可选的,因此您可以跳过它。
但是,如果您的类混合了私有、受保护和公共数据成员,则出于可读性的考虑,指定成员为私有可能是一个好主意。
private is optional, so you can skip it.
However, if your class has a mix of private, protected and public data members, it may be a good idea to specify that a member is private for the sake of readability.
另外,私有字段名称应该采用驼峰式大小写,可以选择带有前缀 _ 或 m_ :
Also private field names should be in camel case, optionaly with prefix _ or m_ :
就我个人而言,如果常量像其他语言一样全部大写,我会喜欢它......我认为这是一种快速而简单的发现常量的方法。 尽管如此,由于 UsePascalCasing 框架中内置了其他常量,因此您也应该这样做。 一致性非常重要。
至于“帕斯卡与骆驼”,你遇到了同样的问题。 如果您只是从头开始自己编程,您可以做任何您想做的事情。 但由于您使用的是预先存在的框架,为了保持一致性,您应该模仿相同的风格。 此外,一旦习惯了,您可能会发现遵循同一组规则实际上会有所帮助,因为您会立即知道某物是参数或局部变量(camelCasing)与属性或常量(PascalCasing) 。
Personally, I would love it if constants were ALL_CAPS like in some other languages...I think that it's an quick and easy way to spot constants. Nevertheless, since other constants built into the framework UsePascalCasing, you should too. Consistency is very important.
As far as "Pascal vs. Camel", you run into the same issue. If you were just programming on your own, from scratch, you could do whatever you wanted. But since you're using a preexisting framework, for the sake of consistency, you should emulate the same style. Additionally, once you get used to it, you'll probably find that following the same set of rules will actually help, because you'll instantly know that something is a parameter or local variable (camelCasing) vs a property or constant (PascalCasing).
来自 .NET Framework 开发人员指南
类型成员的名称
来自 .NET Framework 开发人员指南
大写约定
请注意常量命名中 Pascal 大小写的隐含标准。
从框架设计指南:可重用 .NET 库的约定、习惯用法和模式,第二版第 161 页,
我找不到任何关于是否应该使用术语 private 来修饰私有字段的参考。 我认为这更多的是一种内部风格选择。 无论您选择哪一个,您都希望保持一致。
From .NET Framework Developer's Guide
Names of Type Members
From .NET Framework Developer's Guide
Capitalization Conventions
Note the implied standard of Pascal casing in constant naming.
From Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries, Second Edition page 161
I cannot find any reference to whether you should decorate private fields with the term private. that is more of an internal style choice i would assume. Which ever you pick you will want to stay consistent.
1)我倾向于使用私有,只是为了明确,但我想确实没有必要
2)确实如此,微软建议不要对常量使用上限。
可以在此处找到 Microsoft 类型成员的命名约定 gyuidelines
1) I tend to use private, just to be explicit, but there really is no need to I guess
2) It's true, Microsoft do recommend not using caps for constants.
Microsoft's naming convention gyuidelines for type members can be found here
这是一本包含 C# 和 VB .net 编码指南的免费电子书,非常好
电子书下载链接
但就我个人而言,为了便于阅读,我喜欢明确指定某些内容何时是私有的,事实上,我已经习惯这样做了,以至于当我看不到它时我会感到困惑。 至于常量,我使用 PascalCasing。
Here's a free ebook with C# and VB .net coding guidelines, it's very good
Link to ebook download
Personally though, I like explicitly specifying when something is private, for readability, in fact I'm so used to doing so that when I don't see it I get confused. As for constants, I use PascalCasing.
您可能还对 Microsoft 自己的 .NET Framework 内部编码指南感兴趣,如 :
遵循针对内部和外部成员的所有 .NET Framework 设计指南。 其中的要点包括:
You may also be interested in Microsoft's own Internal Coding Guidelines for the .NET Framework, as revealed by Brad Abrams in his blog:
Follow all .NET Framework Design Guidelines for both internal and external members. Highlights of these include:
@丹·迪普洛
# 不要对成员变量使用前缀(、m、s_ 等)。 如果你想区分局部变量和成员变量,你应该使用“this”。 在 C# 和“我”中。 在 VB.NET 中。
这是很有争议的。 前缀有助于智能感知:您输入前缀字符并仅获取本地私有实例字段的列表。 有了这个。 您将获得一个完整列表,其中包含方法、字段、属性、事件等。
另请考虑以下示例:
@Dan Diplo
# Do not use a prefix for member variables (, m, s_, etc.). If you want to distinguish # between local and member variables you should use “this.” in C# and “Me.” in VB.NET.
That's highly arguable. Prefix helps intellisense: you put prefix char and get a list only of local private instance fields. With this. you will get a full list which consists of methods, fields, properties, events ect.
Consider also following example:
在 C# 中,可见性默认为最有限的可见性。 没有修饰符:
因为无论如何尽可能地限制可见性是一个好主意,所以我尝试总是在默认情况下省略修饰符可见性就是我所需要的。 这使得那些不是默认的成员更加明显,这有助于让我关注他们是否真的需要那么可见。
对于常量,我的偏好是将它们放在自己的类中,这样 ClassName.ConstantName 格式就可以清楚地看出它们是什么。
一般来说,我遵循 Microsoft 的开发类库的设计指南。
In C# visibility defaults to the most limited visibility possible. Without a modifier:
Because it's a good idea to limit visibility as much as possible anyway, I try to always leave off the modifier where the default visibility is all I need. This makes those members that aren't the default more obvious, which helps keep me attentive to whether they really need to be that visible.
For constants, my preference would be to put them in their own class(es) so that the ClassName.ConstantName format makes it obvious what they are.
In general I follow Microsoft's Design Guidelines for Developing Class Libraries.