C # Coding Standard (i)

Source: Internet
Author: User

Always want to write a code of their own standards, after a period of excellent open source code observation and look at other people write standards, feel good code to people feel is comfortable, also very important. So put them in summary, for later forget, in addition, usually write code when you can refer to. The following example is primarily based on Microsoft's code.

Naming conventions
    • PascalCasing

PascalCasing the first letter of each word is capitalized and the remaining letters are lowercase. For example: Fileaccess,arraysegment and so on.

In addition to parameters, variables, constants , names of all namespace names, classes, functions, interfaces, properties, events, enumerations, and so on, use the Pascal style .

    • CamelCasing

CamelCasing the first letter in lowercase and the first letter of the remaining words capitalized. For example: Propertyname,filepath and so on.

Parameters and variables are named using camelcasing.

    • Screaming_caps

Screaming_caps all letters in each word are capitalized, and the words are concatenated with the word "_", which is currently used only for Const constants in C #.

such as: Public const string default_page = "default.aspx";

    • Naming of private variables

Private variables use the casing rules of the underscore "_" or "m_"+camelcasing to quickly confirm the scope of the variable.

such as: < Span style= "color: #ff0000;" >private int UserId ;

  Private int m_userId;

First, naming conventions and styles

1, type name and method name with Pascal naming specification:

     Public class Stringreader:textreader    {     public override  String ReadToEnd ()           {     ...           } }  

2. Local variables and method parameters use the camel naming convention:

Internal Static BOOL    checkhost) {      =...}

3. Interface name plus prefix I:

 Public Interface IComparer {         //  compares-objects. An implementation of this method must return a         //  value less than zero if x are less than Y, zero if X is equal to Y, or a         //  value greater than zero if x is greater than Y.         int Compare (object x, object y);    }

4, the private member variable prefix m_ or _:

    unsafe Internal classPathhelper {//maximum size, max be greater than Max path if contains escape sequence           Private intm_capacity; //Current Length (next character position)           Private intm_length; //Max Path, May is less than capacity           Private int_maxpath; ... ..}

5. Constants using Pascal or ALL caps:

    Private Const intFormat_message_ignore_inserts =0x00000200; Private Const intFormat_message_from_system =0x00001000; Private Const intFormat_message_argument_array =0x00002000; Const stringRSAKeyValue = Xmlsignatureconstantsnamespace +"RSAKeyValue";

6. After the custom attribute class, add the suffix attribute:

[AttributeUsage (Attributetargets.struct | AttributeTargets.Class | attributetargets.assembly, AllowMultiple = True)]
[ComVisible (True)]
public sealed class DebuggerVisualizerAttribute: Attribute

7, custom exception class plus suffix exception:

[Serializable]
public class ArgumentException : SystemException, ISerializable

8. Method naming uses "verb/object" pairs, such as Gethashcode (...),

Public unsafe static Array CreateInstance (Type elementtype, int length)

9, the method name with the return value needs to describe this return value, such as Getobjectstate().

10, the use of descriptive variable name.

A. Avoid using a single letter as a variable name, such as I or T, with index or temp instead.

B. Avoid the use of Hungarian notation for public members or protected members.

C. Avoid using abbreviations (such as num for number).

11. Do not confuse the use of predefined types in C # with the types in the CLR:

such as object and object, string and String,int and Int32

12, for generics, use the type of the first letter instead, when used. NET type, use type as the suffix:

public class Dictionary<k,v>

{...}

13. Use an opinion namespace name, such as product name or company name.

14. Avoid using fully qualified type names. Replace with a using statement.

15. Put all the framework namespaces together on top, place customizations at the bottom, and third-party namespaces in the middle:

using System; using System.Collections; using System.Collections.Generic; using threadpartylibrary; using Mycompanyname;

16, do not show the instantiation of the delegate by the delegate inference:

Delegate void somedelegate ();  Public void  = SomeMethod;

17. Use tab to indent.

18. All member variables must be declared at the top, separated by one line from the property or method.

19. When defining a local variable, try to make it close to the place where it was used for the first time.

20, the file name can reflect the class it uses.

21. When using the opening brace ({), wrap the line.

22, if the following even if there is only one line of code, also add {} surrounded:

23. Create a directory for each namespace. such as MyProject.TestSuite.TestTier using Myproject/testsuite/testtier as the path.

24. When the code in a row is too long, it is recommended that you use the following method to break the line:

    • Break after a comma----comma
    • After a operator----operator
    • Prefer higher-level breaks to lower-level breaks----
    • Align the new line and the beginning of the expression at the same level on the previous line.

such as Longmethodcall (Expr1, EXPR2,

EXPR3, EXPR4, EXPR5);

Examples of breaking an arithmetic expression:

PREFER:

var = a * b/(c-g + f) +         4 * z;    var = a * b/(c-g + f) +    4 *var = a * b/(c-g +          4 ) * Z;

25. Initialize the variable when it is declared.

26. The Framework type inherited from stream ends with a stream:

such as FileStream, MemoryStream and so on.

27. When you do not understand a method, you can break them down into smaller parts and give them proper names.

C # Coding Standard (i)

Related Article

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.