C # Coding specifications summarized by myself-3. Best naming practices in specific scenarios

Source: Internet
Author: User
  • Best naming practices in specific scenarios
    • Namespace
    1. Pascalcasing should be used to separate the parts of the namespace with dots.

      Such as microsof. Office. PowerPoint

    2. Use the company name as the namespace prefix to avoid using the same name as another company.
    3. Use a stable, version-independent product name as the second layer of the namespace
    4. Do not use the company's organizational structure to determine the namespace hierarchy, because the internal organizational structure is often changed.
    5. 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.

  • Generic parameter naming
  1. 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 );

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

  3. If a generic parameter has a constraint, you must display the constraint in the generic parameter name.

    Public interface isessionchannel <tsession> where tsession: isession

  • Name of Enumeration type
  1. Use singular nouns instead of plural names for enumeration types. For example, use consolecolor instead of consolecolors.

    Public Enum consolecolor

    {

    Red,

    Yellow,

    Blue

    }

  2. 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
  1. Use nouns, noun phrases, or adjectives to name attributes.
  2. 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 ;}

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

  • Event name
  1. Use a verb or verb phrase to name an event

    Such as clicked, painting, and droppeddown.

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

  • Field name
    1. Do not use the public and protected fields of the instance. Use properties instead.

    TIPS: Enter "prop" in Visual Studio to quickly create externally modifiable attributes. Enter "propg" to quickly create attributes that cannot be modified externally. For example:

// Propg

Public int nodescount {Get; private set ;}

// Prop

Public list <binarynode> nodes {Get; set ;}

  1. Generally, only static fields are used.
  2. Use a noun, noun phrase, or adjective to name a field
  3. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.