几个C#命名约定问题

发布于 2024-07-29 17:25:01 字数 668 浏览 7 评论 0 原文

1) 声明变量的策略是什么? 您应该始终使用关键字private,还是可以跳过它?

string MyVar1;

我看到的

private string MyVar1;

唯一原因是 Microsoft 有一天可以将默认访问修饰符更改为 public 而不是 private

哪里说 private 是可选的? 是否引用了 MSDN

2) 常量的命名策略?

我在编写常量时总是使用大写字母,但一位朋友告诉我,这违反了微软的命名政策,是吗?

const string MYVAR1;

对比

const string myVar1;

3) 帕斯卡还是骆驼?

就我个人而言,我认为骆驼只是看起来很丑。

1)
What's the policy for declaring a variable? Should you always use the keyword private, or is it OK to skip it?

string MyVar1;

vs.

private string MyVar1;

The only reason I see is that Microsoft one day can change the default access modifiers to public instead of private.

Where does it say that private is optional? Any references to MSDN?

2)
Naming policy for constants?

I have always used caps when writing a constant, but a friend told me that it's against the Microsoft naming policy, is it?

const string MYVAR1;

vs

const string myVar1;

3)
Pascal or Camel?

Personally I think that Camel just looks ugly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

檐上三寸雪 2024-08-05 17:25:01

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 the private 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).

探春 2024-08-05 17:25:01

您可能对 Microsoft 的类库开发人员设计指南< /a>

You might be interested in Microsoft's Design Guidelines For Class Library Developers

所谓喜欢 2024-08-05 17:25:01

不直接回答您的问题,但也许您会对 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.

残花月 2024-08-05 17:25:01

根据 MS 指南命名常量的官方建议是:

  • 对包含一两个字符的名称全部使用大写,即 System.Math.PI、System. Math.E
  • 对于任何等于或超过 3 个字符的内容,请使用 PascalCasing

The official recommendation for naming consts according to the MS guidelines, since no-one has actually specified it fully yet is:

  • Use all caps for names with one or two characters i.e. System.Math.PI, System.Math.E
  • For anything equal to or over 3 characters, use PascalCasing.
慢慢从新开始 2024-08-05 17:25:01

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.

以为你会在 2024-08-05 17:25:01

我怀疑 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.

情归归情 2024-08-05 17:25:01

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.

诺曦 2024-08-05 17:25:01

另外,私有字段名称应该采用驼峰式大小写,可以选择带有前缀 _ 或 m_ :

    private int count;
or
    private string _name;
or
    private decimal m_price;

Also private field names should be in camel case, optionaly with prefix _ or m_ :

    private int count;
or
    private string _name;
or
    private decimal m_price;
记忆之渊 2024-08-05 17:25:01

就我个人而言,如果常量像其他语言一样全部大写,我会喜欢它......我认为这是一种快速而简单的发现常量的方法。 尽管如此,由于 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).

软甜啾 2024-08-05 17:25:01

在字段名称中使用 Pascal 大小写。

来自 .NET Framework 开发人员指南
类型成员的名称

请对所有公众使用帕斯卡大小写
成员、类型和命名空间名称
由多个单词组成。

请注意,此规则不适用于
实例字段。 出于以下原因
详细说明在成员设计中
准则,你不应该使用公共
实例字段。

来自 .NET Framework 开发人员指南
大写约定

请注意常量命名中 Pascal 大小写的隐含标准。

请使用常量字段作为常量
这永远不会改变。

编译器会烧掉 const 的值
字段直接进入调用代码。
因此,const 值永远不能
改变时没有损坏的风险
兼容性。

公共结构 Int32 { 
    公共常量 int MaxValue = 0x7fffffff; 
    公共 const int MinValue = 未选中((int)0x80000000); 
  } 
  

从框架设计指南:可重用 .NET 库的约定、习惯用法和模式,第二版第 161 页,

我找不到任何关于是否应该使用术语 private 来修饰私有字段的参考。 我认为这更多的是一种内部风格选择。 无论您选择哪一个,您都希望保持一致。

Do use Pascal casing in field names.

From .NET Framework Developer's Guide
Names of Type Members

Do use Pascal casing for all public
member, type, and namespace names
consisting of multiple words.

Note that this rule does not apply to
instance fields. For reasons that are
detailed in the Member Design
Guidelines, you should not use public
instance fields.

From .NET Framework Developer's Guide
Capitalization Conventions

Note the implied standard of Pascal casing in constant naming.

DO use constant fields for constants
that will never change.

The compiler burns the values of const
fields directly into calling code.
Therefore, const values can never be
changed without the risk of breaking
compatibility.

public struct Int32 {
  public const int MaxValue = 0x7fffffff;
  public const int MinValue = unchecked((int)0x80000000);
}

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.

意中人 2024-08-05 17:25:01

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

还在原地等你 2024-08-05 17:25:01

这是一本包含 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.

情栀口红 2024-08-05 17:25:01

您可能还对 Microsoft 自己的 .NET Framework 内部编码指南感兴趣,如

遵循针对内部和外部成员的所有 .NET Framework 设计指南。 其中的要点包括:

  • 不要使用匈牙利表示法
  • 不要使用成员变量的前缀(、m、s_ 等)。 如果你想区分
  • 局部变量和成员变量,你应该使用“this”。 在 C# 和“我”中。 在 VB.NET 中。
  • 成员变量使用驼峰式命名
  • 参数使用驼峰式
  • 命名 局部变量使用驼峰式
  • 命名 函数、属性、事件和类名使用 PascalCasing
  • 接口名称前添加“I”
  • 请勿在枚举、类或委托前添加任何字母

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:

  • Do not use Hungarian notation
  • 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.
  • Do use camelCasing for member variables
  • Do use camelCasing for parameters
  • Do use camelCasing for local variables
  • Do use PascalCasing for function, property, event, and class names
  • Do prefix interfaces names with “I”
  • Do not prefix enums, classes, or delegates with any letter
回眸一遍 2024-08-05 17:25:01

@丹·迪普洛
# 不要对成员变量使用前缀(、m、s_ 等)。 如果你想区分局部变量和成员变量,你应该使用“this”。 在 C# 和“我”中。 在 VB.NET 中。

这是很有争议的。 前缀有助于智能感知:您输入前缀字符并仅获取本地私有实例字段的列表。 有了这个。 您将获得一个完整列表,其中包含方法、字段、属性、事件等。

另请考虑以下示例:

private int _count; 
private int total; 
private decimal price; 

public MyClass(int count, int total, decimal price) 
{ 
    _count = count;     // correct 
    this.total = total; // correct 
    price = price;      // wrong! you forgot this. qualifier 
} 

@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:

private int _count; 
private int total; 
private decimal price; 

public MyClass(int count, int total, decimal price) 
{ 
    _count = count;     // correct 
    this.total = total; // correct 
    price = price;      // wrong! you forgot this. qualifier 
} 
全部不再 2024-08-05 17:25:01

在 C# 中,可见性默认为最有限的可见性。 没有修饰符:

  • 非内部类是内部
  • 内部类是私有
  • 类成员是私有的

因为无论如何尽可能地限制可见性是一个好主意,所以我尝试总是在默认情况下省略修饰符可见性就是我所需要的。 这使得那些不是默认的成员更加明显,这有助于让我关注他们是否真的需要那么可见。

对于常量,我的偏好是将它们放在自己的类中,这样 ClassName.ConstantName 格式就可以清楚地看出它们是什么。

一般来说,我遵循 Microsoft 的开发类库的设计指南

In C# visibility defaults to the most limited visibility possible. Without a modifier:

  • non-inner classes are internal
  • inner classes are private
  • class members are private

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文