C# 缩写词的命名约定

发布于 2024-08-18 11:13:18 字数 94 浏览 2 评论 0原文

关于 C# 缩写词的命名,如果我正在编写一个与 Windows API 相关的库,是否有针对 WindowsApi 或 WindowsAPI 的强烈约定,或者这只是个人偏好?

Regarding C# naming for acronyms, if I was writing a library related to the Windows API is there any strong convention toward either WindowsApi or WindowsAPI or is it just personal preference?

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

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

发布评论

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

评论(8

若能看破又如何 2024-08-25 11:13:18

有一个约定,它指定所有长度超过 2 个字符的首字母大写,其余小写。因此HttpContextClientID

There is a convention, and it specifies initial uppercase, the rest lowercase, for all acronyms that are more than 2 characters long. Hence HttpContext and ClientID.

沫尐诺 2024-08-25 11:13:18

框架设计指南”第二版,作者:Krzysztof Cwalina 和 Brad Abrams,第 40 页- 42

3.1.2 首字母缩略词大写

DO 将双字符首字母缩略词中的两个字符都大写,除了驼峰式标识符的第一个单词。

System.IO
public void StartIO(Stream ioStream)

DO 仅将包含三个或更多字符的首字母缩略词的第一个字符大写,驼峰式标识符的第一个单词除外。

System.Xml
public void ProcessHtmlTag(string htmlTag)

请勿在驼峰式标识符的开头大写任何首字母缩略词的任何字符,无论其长度如何。

"Framework Design Guidelines" 2nd edition by Krzysztof Cwalina and Brad Abrams pp.40-42

3.1.2 Capitalizing Acronyms

DO capitalize both characters on two-character acronyms, except the first word of a camel-cased identifier.

System.IO
public void StartIO(Stream ioStream)

DO capitalize only the first character of acronyms with three or more characters, except the first word of a camel-cased identifier.

System.Xml
public void ProcessHtmlTag(string htmlTag)

DO NOT capitalize any of the characters of any acronyms, whatever their length, at the beginning of a camel-cased identifier.

橘香 2024-08-25 11:13:18

老问题,新答案。

根据 .NET 4 首字母缩略词大写规则

双字符首字母缩略词的两个字符都大写,除了
驼峰式标识符的第一个单词。

名为 DBRate 的属性是一个缩写词 (DB) 的示例,用作
Pascal 大小写标识符的第一个单词。一个名为
ioChannel 是用作第一个单词的短首字母缩略词 (IO) 的示例
驼峰式标识符。

仅将三个或更多首字母缩略词的第一个字符大写
字符,驼峰式标识符的第一个单词除外。

名为 XmlWriter 的类是用作长首字母缩略词的示例
Pascal 大小写标识符的第一个字。名为 htmlReader 的参数
是用作第一个单词的长首字母缩略词的示例
驼峰式标识符。

不要将任何首字母缩略词的任何字符大写,无论是什么
它们的长度,位于驼峰式标识符的开头。

名为 xmlStream 的参数是使用长首字母缩略词 (xml) 的示例
作为驼峰式标识符的第一个单词。一个名为
dbServerName 是用作第一个缩写词 (db) 的示例
驼峰式标识符的单词。

Old question, new answer.

According to .NET 4 Capitalization Rules for Acronyms:

Do capitalize both characters of two-character acronyms, except the
first word of a camel-cased identifier.

A property named DBRate is an example of a short acronym (DB) used as
the first word of a Pascal-cased identifier. A parameter named
ioChannel is an example of a short acronym (IO) used as the first word
of a camel-cased identifier.

Do capitalize only the first character of acronyms with three or more
characters, except the first word of a camel-cased identifier.

A class named XmlWriter is an example of a long acronym used as the
first word of a Pascal-cased identifier. A parameter named htmlReader
is an example of a long acronym used as the first word of a
camel-cased identifier.

Do not capitalize any of the characters of any acronyms, whatever
their length, at the beginning of a camel-cased identifier.

A parameter named xmlStream is an example of a long acronym (xml) used
as the first word of a camel-cased identifier. A parameter named
dbServerName is an example of a short acronym (db) used as the first
word of a camel-cased identifier.

萝莉病 2024-08-25 11:13:18

查看 Microsoft 的官方文档 命名指南和大写约定

要区分标识符中的单词,请将标识符中每个单词的第一个字母大写。不要使用下划线来区分单词,或者在标识符中的任何地方使用下划线。根据标识符的用途,有两种适当的方法将标识符大写:

  • PascalCasing
  • 驼峰式

PascalCasing 约定用于除参数名称之外的所有标识符,将每个单词的第一个字符大写(包括长度超过两个字母的首字母缩略词),如以下示例所示:

  • 属性描述符
  • HtmlTag

双字母缩写词有一种特殊情况,其中两个字母都大写,如以下标识符所示:

  • IOStream

camelCasing 约定仅用于参数名称,将除第一个单词之外的每个单词的第一个字符大写,如以下示例所示。如示例所示,以驼峰式标识符开头的两个字母首字母缩略词均为小写。

  • propertyDescriptor
  • ioStream
  • htmlTag

✓ 应对所有由多个单词组成的公共成员、类型和命名空间名称使用 PascalCasing。

✓ 应使用驼峰命名法作为参数名称。

Check Microsoft's official docs on Naming Guidelines & Capitalization Conventions:

To differentiate words in an identifier, capitalize the first letter of each word in the identifier. Do not use underscores to differentiate words, or for that matter, anywhere in identifiers. There are two appropriate ways to capitalize identifiers, depending on the use of the identifier:

  • PascalCasing
  • camelCasing

The PascalCasing convention, used for all identifiers except parameter names, capitalizes the first character of each word (including acronyms over two letters in length), as shown in the following examples:

  • PropertyDescriptor
  • HtmlTag

A special case is made for two-letter acronyms in which both letters are capitalized, as shown in the following identifier:

  • IOStream

The camelCasing convention, used only for parameter names, capitalizes the first character of each word except the first word, as shown in the following examples. As the example also shows, two-letter acronyms that begin a camel-cased identifier are both lowercase.

  • propertyDescriptor
  • ioStream
  • htmlTag

✓ DO use PascalCasing for all public member, type, and namespace names consisting of multiple words.

✓ DO use camelCasing for parameter names.

娇纵 2024-08-25 11:13:18

我听说你应该避免缩写,所以它会变成WindowsApplicationProgrammingInterface

更严重的是(人们似乎误读了上面的内容,尽管下面引用了),此页面 说:

任何三个或更多字母的首字母缩略词都应采用帕斯卡大小写,而不是全部大写。

由于 API 被认为是众所周知的首字母缩略词,因此如果您想遵循指南,则可以选择名称 WindowsApi

I've heard that you should avoid abbreviations, so it would become WindowsApplicationProgrammingInterface, then.

More seriously (folks seem to be mis-reading the above, despite the quote below), this page says:

Any acronyms of three or more letters should be Pascal case, not all caps.

Since API is considered a well-known acronym, the name WindowsApi is the one to pick if you want to follow the guidelines.

冰火雁神 2024-08-25 11:13:18

其个人喜好。但 .NET 将使用 WindowsApi。它类似于 TcpClient 的命名。

Its personal preference. But .NET would use WindowsApi. It is akin to the naming of TcpClient.

橘虞初梦 2024-08-25 11:13:18

这只是个人(或组织)偏好。只要你保持一致,就没有问题。

.NET Framework 本身将使用WindowsApi。

It's all just personal (or organizational) preference. As long as you're consistent, you'll be ok.

The .NET Framework itself would use WindowsApi.

守望孤独 2024-08-25 11:13:18

也看看 FxCop。这是一个很好的实用程序,可以帮助解决此类问题。

Take a look at FxCop too. It's a nice utility that will help with issues like this.

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