C# 中的命名约定

发布于 2024-08-08 18:08:44 字数 1431 浏览 2 评论 0原文

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

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

发布评论

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

评论(6

行雁书 2024-08-15 18:08:44

Microsoft 有一套出色的关于类库设计的指南,包括< href="http://msdn.microsoft.com/en-us/library/ms229002.aspx" rel="noreferrer">有关命名的部分。简而言之(括号中的示例):

  • 类/结构:PascalCase (WebRequest)
  • 接口:带有 I 前缀的 PascalCase (IDisposable)
  • 方法:PascalCase (ToUpper) >)
  • 属性:PascalCase (Length)
  • 事件:PascalCase (Click)
  • 命名空间:PascalCase (System.Collections;两个单词合而为一的情况并不常见 包括参数在内
  • 的非常量变量:camelCased (keySelector)
  • 常量:PascalCase (Int32.MaxValue)
  • 枚举:PascalCase,单数表示非标志,复数表示标志 ( HttpStatusCode, BindingFlags)
  • 属性:带“Attribute”后缀的 PascalCase (ThreadStaticAttribute)

私有名称由您决定,但我倾向于遵循相同的名称与其他一切一样的约定。尽管许多地方使用“m_”或“_”作为实例变量的前缀,但不鼓励使用匈牙利表示法(Win32 风格)。

Microsoft has an excellent set of guidelines on class library design, including a section on naming. In short (examples in parentheses):

  • Classes/Structs: PascalCase (WebRequest)
  • Interfaces: PascalCase with I prefix (IDisposable)
  • Methods: PascalCase (ToUpper)
  • Properties: PascalCase (Length)
  • Events: PascalCase (Click)
  • Namespaces: PascalCase (System.Collections; unusual to have two words in one part though)
  • Non-constant variables including parameters: camelCased (keySelector)
  • Constants: PascalCase (Int32.MaxValue)
  • Enums: PascalCase, singular for non-flags and plural for flags (HttpStatusCode, BindingFlags)
  • Attributes: PascalCase with "Attribute" suffix (ThreadStaticAttribute)

Private names are up to you, but I tend to follow the same conventions as for everything else. Hungarian notation (in the style of Win32) is discouraged, although many places use "m_" or "_" as a prefix for instance variables.

耳钉梦 2024-08-15 18:08:44

Resharper 的指南建议


  • 类型和命名空间 UpperCamelCase
  • 接口 IUpperCamelCase
  • 类型参数 TUpperCamelCase
  • 方法属性和事件 UpperCamelCase
  • 局部变量 lowerCamelCase
  • 本地常量 lowerCamelCase
  • 参数 lowerCamelCase
  • 字段(非私有) UpperCamelCase
  • 实例字段(私有) _lowerCamelCase
  • 静态字段(私有)_lowerCamelCase
  • 常量字段(非私有)UpperCamelCase
  • 常量字段(私有)UpperCamelCase
  • 静态只读字段(非私有) UpperCamelCase
  • 静态只读字段(私有) UpperCamelCase
  • 枚举成员 UpperCamelCase
  • 所有其他实体 UpperCamelCase

Resharper's guidelines suggest


  • Types and namespaces UpperCamelCase
  • Interfaces IUpperCamelCase
  • Type parameters TUpperCamelCase
  • Methods properties and events UpperCamelCase
  • Local variables lowerCamelCase
  • Local constants lowerCamelCase
  • Parameters lowerCamelCase
  • Fields (not private) UpperCamelCase
  • Instance fields (private) _lowerCamelCase
  • Static field (private) _lowerCamelCase
  • Constant fields (not private) UpperCamelCase
  • Constant fields (private) UpperCamelCase
  • Static readonly fields (not private) UpperCamelCase
  • Static readonly fields (private) UpperCamelCase
  • Enum members UpperCamelCase
  • All other entities UpperCamelCase
凌乱心跳 2024-08-15 18:08:44

The .NET standard from Microsoft is to use Pascal Case for namespaces, public and protected members (basically anything visible to other classes). For private members and local variables, there's a much wider berth to just do whatever you and your team are most comfortable with.

愛上了 2024-08-15 18:08:44

不要低估尽可能严格遵循您正在使用的平台的命名约定的价值。

查看 .NET Framework 的参考材料,了解如何“适应”的示例 (http://msdn.microsoft.com/en-us/library/ms229335.aspx)。

Jon Skeet 为您提供了 Microsoft 一篇精彩文章的链接:http:// msdn.microsoft.com/en-us/library/ms229042.aspx

您还可以使用独立的 Microsoft FxCop(或代码分析,如果您有团队版)http://www.microsoft.com/downloads/details.aspx?FamilyID= 9aeaa970-f281-4fb0-aba1-d59d7ed09772&DisplayLang=en 检查是否遵循命名约定。它具有针对 Microsoft 约定的内置规则,这是您应该使用它们的另一个原因!

Don't underestimate the value of following the naming conventions of the platform you are working on as closely as possible.

Look at the reference material for the .NET Framework for examples of how to "fit in" (http://msdn.microsoft.com/en-us/library/ms229335.aspx).

Jon Skeet has given you a link to a good writeup by Microsoft: http://msdn.microsoft.com/en-us/library/ms229042.aspx

You can also use the standalone Microsoft FxCop (or Code Analysis if you have the Team Edition) http://www.microsoft.com/downloads/details.aspx?FamilyID=9aeaa970-f281-4fb0-aba1-d59d7ed09772&DisplayLang=en to check that the naming conventions have been followed. It has built-in rules for the Microsoft conventions, which is another reason you should be using them!

煮茶煮酒煮时光 2024-08-15 18:08:44

Juval Lowy 在 Programming .NET Components 中对此进行了尝试,请参阅此SO 链接

Juval Lowy took a stab at this is in Programming .NET Components, see this SO link too.

_蜘蛛 2024-08-15 18:08:44

我想看一本名为“C# 风格的元素”的薄书” 作者:鲍德温、格雷和米斯费尔特。蓝皮书涵盖了命名约定以及创建一致、干净、可读代码的许多其他方面。

I'd have a look at the slim book called "Elements of C# Style" by Baldwin, Gray, & Misfeldt. The blue book covers naming conventions, and many other aspects of creating consistent, clean, readable code.

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