C# naming conventions for acronyms

2019-01-16 12:00发布

问题:

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?

回答1:

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.



回答2:

"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.



回答3:

Check microsoft official naming guidelines here Naming Guidelines



回答4:

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.



回答5:

Old question, new answer.

According to the "Capitalization Rules for Acronyms" section of the MSDN Capitalization Conventions article:

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.



回答6:

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



回答7:

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.



回答8:

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