by Mark Zhou
19. October 2010 19:34
This is a very important rule. Identifiers should not contain underscores (_) in anywhere of their names. This is a standard that we called naming convention.
For example, the following identifiers will cause this rule failed to validate.
- _name
- Name_Of_The_Book
- dictionary_
And this rule will check the identifiers for namespaces, types (that is, class, struct, delegate, interface and enum), members (that is, property, public field, event, method) and the method parameters. Note, the static members of a type also compliant to this rule.
Recall the naming convention. it provides a list that contains several rules for naming an identifier for different context (cush as namespaces, classes or interfaces), if you are writing code to the Common Language Runtime (CLR) and want to make your code reusable to other .NET developers, it is highly recommended that you follow all the naming conventions so that the costs of learning courses for the software libraries could be reduced. Here are some standard naming conventions for the Common Language Specification (CLS) compliant languages that can be used to develop code on top of the .NET platform.
| Identifier Kind | Naming Convention | Example |
| namespace | PascalNaming | Microsoft.VisualStudio |
| class | PascalNaming | ExtensionManager |
| interface | IPascalNaming | IExtensible |
| struct | PascalNaming | BigInteger |
| enum | PascalNaming | Weekday |
| delegate | PascalNaming | EventHandler<TEventArgs> |
| private field | camelNaming | name, _cachedDictionary |
| public field | PascalNaming | Empty, Missing |
| static field | camelNaming | syncObject |
| method parameter | camelNaming | ajaxOptions |
| property | PascalNaming | TargetSite |
| event | PascalNaming | Click |
| method | PascalNaming | GetFromGuid(Guid guidValue) |
| event handler | OnPascalNaming | OnDeactivated, OnClick, OnFormClosing |
| local variable | camelNaming | theList, j, oldValue |
| lambda expression parameter | camelNaming | (x, y) => x + y |
| Exception-derived type | PascalNamingException | ArgumentNullException, IOException |
| event handler delegate | PascalNamingEventHandler | AsyncCallBackEventHandler |
| control identifier | -ControlType suffix | local variable: myTextBox, okButton field: _mainMenu property: UserNameTextBox |
| EventArgs-derived type | -EventArgs suffix | DoWorkEventArgs |
| type parameter | PascalNaming with prefix T | TKey, TValue, or T |
| indexed property | camelNaming | this[int index] |
| constant | PascalNaming | Pi, MaxInteger |
Note:
- Pascal Naming is a naming convention where each letter begins the term of the compound word should be uppercased. For example, DoWorkCompleteEventArgs
- Camel Naming is a naming convention where the first letter of the beginning word of the compound word is lowercased and the other first letters of the remaining terms of the compound word is uppercased. For example, doWorkCompleteDelegate
- There is another rule for the beginning underscore (_) for the private field, I will discover this in later posts
- Generally, if the name of the identifiers have a suffix, then the suffix should be the type of this identifier (like a user control), or the base type of this identifier (like an Exception-derived type).
The PascalNaming or camelNaming conventions don’t expect any underscore inside the identifier, so, avoid putting underscores as a prefix, a part of the name, or suffix to any identifiers. To fix this rule, simply remove all the underscores from your identifiers, and again, do not suppress this rule at any circumstance.
Even in resource strings, the underscore is also not allowed.
To get more information, please see this MSDN link for CA1707.
UPDATE on 10/24/2010:
- Added more code parts name conventions to the table