- Best naming practices in specific scenarios
- Pascalcasing should be used to separate the parts of the namespace with dots.
Such as microsof. Office. PowerPoint
- Use the company name as the namespace prefix to avoid using the same name as another company.
- Use a stable, version-independent product name as the second layer of the namespace
- Do not use the company's organizational structure to determine the namespace hierarchy, because the internal organizational structure is often changed.
- Do not use the same name for the namespace and the type in the space.
For example, do not name the namespace as debug first, and then provide the debug class in the space. Most compilers include vs, which requires users to add a complete qualifier before such a type.
- The interface name must start with the letter I.
For example, icomponet and idisposable are interfaces.
Make sure that if a class is a standard implementation of an interface, the class and interface should have only one "I" prefix.
- The base class name is used at the end of the derived class.
For example, the framework type inherited from stream ends with stream, and the type inherited from exception ends with exception.
- Use descriptive names to name generic parameters, and add the T prefix
For example, the Names below are all good.
Public Delegate toutput converter <tinput, toutput> (tinput from );
- If there is only one type parameter, only one letter T can be used to represent the generic type.
Public class nullable <t>
Public class list <t>
- If a generic parameter has a constraint, you must display the constraint in the generic parameter name.
Public interface isessionchannel <tsession> where tsession: isession
- Use singular nouns instead of plural names for enumeration types. For example, use consolecolor instead of consolecolors.
Public Enum consolecolor
{
Red,
Yellow,
Blue
}
- Do not add suffixes such as "Enum" and "flag" to enumeration types.
Colorenum and colorflag are not good because they are enumeration, and they are meaningless duplicates.
- Use the verb and verb phrase naming method
- Attribute name
- Use nouns, noun phrases, or adjectives to name attributes.
- The attribute set should be named in the plural form of the phrase describing the specific content in the Set, instead of the suffix "list", "array" or "Collection" in the singular form of the phrase
Class binarytree
{
// Good naming
Public nodecollection nodes {Get; set ;}
// Bad naming
Public nodecollection nodescollection {Get; set ;}
- Use affirmative phrases to name the Boolean attribute. We recommend that you add prefix "is", "can", "has" and so on.
Canseek is more accurate and easier to understand than cantseek and seekable.
- Use a verb or verb phrase to name an event
Such as clicked, painting, and droppeddown.
- The concepts before and after an event should be granted to the current running (ing) and past (ed. Instead of using before and after.
For example, the close event before the window is closed should be named closing, and the close event after the window is closed should be named closed.
// Propg
Public int nodescount {Get; private set ;}
// Prop
Public list <binarynode> nodes {Get; set ;}
- Generally, only static fields are used.
- Use a noun, noun phrase, or adjective to name a field
- Do not add a field prefix such as "g _" or "s _" to indicate static fields. Because fields and attributes are very similar, they must follow the same naming rules.