首字母缩略词的 C# 命名约定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2109171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:34:23  来源:igfitidea点击:

C# naming conventions for acronyms

c#naming-conventions

提问by deltanovember

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?

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

采纳答案by dxh

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

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

回答by Daniel A. White

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

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

回答by unwind

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

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

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

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

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

任何三个或更多字母的首字母缩略词都应该是 Pascal 大小写,而不是全部大写。

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

由于 API 被认为是一个众所周知的首字母缩略词,因此WindowsApi如果您想遵循这些准则,则可以选择该名称。

回答by John Saunders

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.

.NET Framework 本身将使用 WindowsApi。

回答by Feryt

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 PascalCasingconvention, 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 camelCasingconvention, 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

? DOuse PascalCasing for all public member, type, and namespace names consisting of multiple words.

? DOuse camelCasing for parameter names.

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

  • 帕斯卡套管
  • 驼色肠衣

PascalCasing惯例,用于除参数名称的所有标识符,大写每个单词(包括长度超过两个字母缩写词)的第一个字符,如图以下实施例:

  • PropertyDescriptor
  • HtmlTag

对于两个字母都大写的双字母首字母缩略词,有一种特殊情况,如以下标识符所示:

  • IOStream

仅用于参数名称的camelCasing约定将每个单词的第一个字符(第一个单词除外)大写,如以下示例所示。该示例还显示,以驼峰式标识符开头的双字母首字母缩略词都是小写的。

  • propertyDescriptor
  • ioStream
  • htmlTag

? 对于由多个单词组成的所有公共成员、类型和命名空间名称,请务必使用 PascalCasing。

? 务必使用驼峰式大小写作为参数名称。

回答by Sergey Teplyakov

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

框架设计指南”第 2 版 Krzysztof Cwalina 和 Brad Abrams pp.40-42

3.1.2 Capitalizing Acronyms

3.1.2 首字母缩略词大写

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

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

System.IO
public void StartIO(Stream ioStream)

DOcapitalize 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 NOTcapitalize any of the characters of any acronyms, whatever their length, at the beginning of a camel-cased identifier.

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

回答by Tim Scarborough

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

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

回答by ElliotSchmelliot

Old question, new answer.

老问题,新答案。

According to .NET 4 Capitalization Rules for Acronyms:

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

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

A property named DBRateis an example of a short acronym (DB) used as the first word of a Pascal-cased identifier. A parameter named ioChannelis 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 XmlWriteris an example of a long acronym used as the first word of a Pascal-cased identifier. A parameter named htmlReaderis 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 xmlStreamis an example of a long acronym (xml) used as the first word of a camel-cased identifier. A parameter named dbServerNameis an example of a short acronym (db) used as the first word of a camel-cased identifier.

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

命名的属性DBRateDB用作 Pascal 大小写标识符的第一个单词的短首字母缩略词 ( )的示例。命名的参数 ioChannelIO用作驼峰式标识符的第一个单词的短首字母缩略词 ( )的示例。

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

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

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

命名的参数xmlStreamxml用作驼峰式标识符的第一个单词的长首字母缩略词 ( )的示例。命名的参数 dbServerNamedb用作驼峰式标识符的第一个单词的短首字母缩略词 ( )的示例。