Work@Microsoft    Live@Seattle

The Ultimate .NET Naming Conventions

The Ultimate .NET Naming Conventions
5 (100%) 1 vote

How do we name objects, classes, interfaces, etc in .NET land?

A consistent naming pattern is one of the most important elements of predictability and discoverability in a managed class library. Widespread use and understanding of these naming guidelines should eliminate many of the most common user questions.  For these reasons and also to promote maintainability, readability, code reviews, improved learning curves, and improve general efficiency.

General Naming Conventions

Do use meaningful names for various types, functions, variables, constructs and data structures. Their use should be plainly discernable from their name alone.

Single-character variables should only be used as counters (i, j) or as coordinates (x, y, z). As a rule-of-thumb a variable should have a more descriptive name as its scope increases.

Use of Abbreviations

  • Do not use abbreviations or contractions as parts of identifier names. For example, use “GetWindow” instead of “GetWin”.
  • Do not use acronyms that are not generally accepted in the computing field.
  • Where appropriate, use well-known acronyms to replace lengthy phrase names. For example, use UI for User Interface and OLAP for On-line Analytical Processing.
  • When using acronyms, use Pascal case or camel case for acronyms more than two characters long. For example, use “”HtmlButton”” or “htmlButton”. However, you should capitalize acronyms that consist of only two characters, such as System.IO instead of System.Io.
  • Do not use abbreviations in identifiers or parameter names. If you must use abbreviations, use camel case for abbreviations that consist of more than two characters, even if this contradicts the standard abbreviation of the word.

Capitalization Styles

Use the following four conventions for capitalizing identifiers. This section can be considered a reference for capitalization styles.

  1. Pascal Case
    The first letter in the identifier and the first letter of each subsequent concatenated word are capitalized. You can use Pascal case for identifiers of three or more characters. For example:  ‘UserName’
  2. Camel Case
    The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized. For example:  ‘userName’
  3. Upper Case
    All letters in the identifier are capitalized. Use this convention only for identifiers that consist of two or fewer letters. For example:  System.IO, and System.Web.UI
  4. All Caps with Underlines
    Many developers are used to naming constants with all uppercase words separated by underscore. For example: private const int CLIENT_WIDTH = 1024;  But in this guideline, we recommend Pascal case for constants.

Capitalization Rules and Naming Convention

The following naming convention is based on the official Microsoft .NET Framework source code in GitHub.

IdentifierCapitalization StyleNotesExample
InterfacePascalPrefixed with an IIPerson
AttributePascalAppend “Attribute”ObsoleteAttribute
ExceptionPascalAppend “Exception”FileDoesNotExistException
Event ArgsPascalAppend “EventArgs”ConsoleCancelEventArgs
DelegatePascalAppend “EventHandler”ConsoleCancelEventHandler
Enum typePascalUse a singular name rather than plural.ConsoleColor
Enum valuePascalBlack
Static FieldCamelPrefix with _private static TextReader _in;
FieldCamelPrefix with _private readonly ConsoleSpecialKey _type
Local VariableCameli
ConstantsPascalprivate const int DefaultConsoleBufferSize = 256;

Leave a Comment

Your email address will not be published. Required fields are marked *