C # Coding specification 1

Source: Internet
Author: User
Naming Conventions and Styles

  1. Use Pascal casing for type and method names and constants:

    public class SomeClass
    {
    const int DefaultSize = 100;
    public SomeMethod( )
    {}
    }

  2. Use camel casing for local variable names and method
    Arguments:

    int number;
    void MyMethod(int someNumber)
    {}

  3. Prefix interface namesI:

    interface IMyInterface
    {..}

  4. Prefix private member variablesM _.

  5. Suffix custom attribute classes
    Attribute.

  6. Suffix custom exception classes
    Exception.

  7. Name methods using verb/object pairs, suchShowDialog (
    ).

  8. Methods with return values shoshould have names describing
    Values returned, suchGetObjectState ().

  9. Use descriptive variable names.

    1. Avoid single-character variable names, suchIOr
      T. UseIndexOrTempInstead.

    2. Avoid using Hungarian notation for public or protected
      Members.

    3. Avoid abbreviating words (suchNumInstead
      Number).

  10. Always use C # predefined types, rather than the aliases in
    SystemNamespace. For example:

    object NOT Object
    string NOT String
    intNOT Int32

  11. With generics, use capital letters for types. Reserve suffixing
    TypeFor when dealing with the. NET typeType:

    //Correct:
    public class LinkedList<K,T>
    {...}
    //Avoid:
    public class LinkedList<KeyType,DataType>
    {...}

  12. Use meaningful namespace names, such as the product name or
    Company name.

  13. Avoid fully qualified type names. UseUsing
    Statement instead.

  14. Avoid puttingUsingStatement inside
    Namespace.

  15. Group all framework namespaces together and put custom or
    Third-party namespaces underneath:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using MyCompany;
    using MyControls;

  16. Use delegate inference instead of explicit delegate
    Instantiation:

    delegate void SomeDelegate( );
    public void SomeMethod( )
    {...}
    SomeDelegate someDelegate = SomeMethod;

  17. Maintain strict indentation. Do not use tabs or nonstandard
    Indentation, such as one space. Recommended values are three or four
    Spaces.

  18. Indent comments at the same level of indentation as the code
    That you are documenting.

  19. All comments shocould pass spellchecking. Misspelled comments
    Indicate sloppy development.

  20. All member variables shocould be declared at the top, with one
    Line separating them from the properties or methods:

    public class MyClass
    {
    int m_Number;
    string m_Name;

    public void SomeMethod1( )
    {}
    public void SomeMethod2( )
    {}
    }

  21. Declare a local variable as close as possible to its first
    Use.

  22. A filename shocould reflect the class it contains.

  23. When using partial types and allocating a part per file, name
    Each file after the logical part that part plays. For example:

    //In MyClass.cs
    public partial class MyClass
    {...}
    //In MyClass.Designer.cs
    public partial class MyClass
    {...}

  24. Always place an open curly brace ({) In a new
    Line.

  25. With anonymous methods, mimic the code layout of a regular
    Method, aligned with the anonymous delegate declaration (this complies
    Placing an open curly brace in a new line ):

    delegate void SomeDelegate(string someString);
    //Correct:
    public void InvokeMethod( )
    {
    SomeDelegate someDelegate = delegate(string name)
    {
    MessageBox.Show(name);
    };
    someDelegate("Juval");
    }
    //Avoid
    public void InvokeMethod( )
    {
    SomeDelegate someDelegate = delegate(string name){MessageBox.Show(name);};
    someDelegate("Juval");
    }

  26. Use empty parentheses on parameter-less anonymous methods. Omit
    The parentheses only if the anonymous method cocould have been used on any
    Delegate:

    delegate void SomeDelegate( );
    //Correct
    SomeDelegate someDelegate1 = delegate( )
    {
    MessageBox.Show("Hello");
    };
    //Avoid
    SomeDelegate someDelegate1 = delegate
    {
    MessageBox.Show("Hello");
    };

 

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.