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.
Use the following four conventions for capitalizing identifiers. This section can be considered a reference for capitalization styles.
- 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’
- Camel Case
The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized. For example: ‘userName’
- 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
- 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.
|Interface||Pascal||Prefixed with an I||IPerson|
|Event Args||Pascal||Append “EventArgs”||ConsoleCancelEventArgs|
|Enum type||Pascal||Use a singular name rather than plural.||ConsoleColor|
|Static Field||Camel||Prefix with _||private static TextReader _in;|
|Field||Camel||Prefix with _||private readonly ConsoleSpecialKey _type|
|Constants||Pascal||private const int DefaultConsoleBufferSize = 256;|