C # Coding Standard

Source: Internet
Author: User
Tags anonymous class definition exception handling instance method reference visual studio
Standard in previous versions of Visual Studio, Microsoft had suggested that the Hungarian nomenclature should be used to write code. In the recently released. NET and its programming language, Microsoft changed his rule.



1. Use Pascal style for format and method names [1]

public class SomeClass



{



Public SomeMethod () {}



}



2. Use camel style for local variables and method parameters [2]

int number;



void MyMethod (int somenumber)



{}



3. Use I as the interface prefix

Interface IMyInterface



{..}



4. Use m_ as a private member variable prefix

public class SomeClass



{



private int m_number;



}



5. Use attribute as the suffix of custom attribute class

6. Use exception as a suffix for custom exception handling classes

7. Use the verb form to name the method name, such as ShowDialog ()



8. A method with a return value should have a method to describe the return value, such as Getobjectstatus ()



9. Use variable names that are easy to describe



L Avoid using single characters to describe variable names, such as I or T. Use index or temp instead



• Avoid using Hungarian nomenclature for shared and protected members [3]



L never use abbreviated characters, such as using num instead of number



10. Always use C # predefined formats without using aliases in system space



Object Not 0bject



String Not string



int not Int32



11. General, for the format of the use of uppercase letters when processing. NET format type, use the type suffix



Correct:



public class linkedlist<k, t>



{..}



Avoid:



public class Linkedlist<keytype,datatype>



{..}



12. Use an understandable namespace [4] name, such as product or company name



13. To avoid using a fully qualified name for a namespace, you should use a using declaration instead



14. Avoid using declarations to be placed inside namespaces



15. Group All frame namespaces, placing users or third party namespaces under them



Using System;



Using System.Collections;



Using System.ComponentModel;



Using System.Data;



Using MyCompany;



Using MyControls;



16. Use delegate reference [5] instead of an explicit delegate instance



delegate void Somedelegate ();



public void SomeMethod ()



{..}



Somedelegate Somedelegate=somemethod;



17. Keep the Strict indent



L use 3 spaces for indentation



L Never use tab or non-standard indentation, such as 1,2 or 4 spaces



18. Position the annotation at the same level as the Code indent



19. All comments should be checked by spell check. Misspelled annotations will herald the development of redundancy



20. All the member variables should be declared at the beginning and separate the properties from the method area using a single line



public class MyClass



{



int m_number;



String M_name;







public void Somemethodl ()



{}



public void SomeMethod2 ()



{}



}



21. Declare the local variable as close as possible to the first time the variable is used



22. The name of the file should reflect the class it contains



23. When using the partial class [6] and assigning it to a portion of each file, name the suffix p with an extra number for each file



In MyClassP1.cs



public partial class MyClass



{..}



In MyClassP2.cs



public partial class MyClass



{..}



24. Place the bracket ({) With the new line



25. For anonymous methods referencing a regular code plan, the indentation should be aligned with the anonymous delegate declaration



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. For methods of anonymous default parameters, you should use an empty bracket representation.



delegate void Somedelegate ();



Correct



Somedelegate Somedelegate1=delegate ()



{



MessageBox.Show ("Hello");



};



Avoid



Somedelegate somedelegate1=delegate



{



MessageBox.Show ("Hello");
};



Due to my limited E, please indicate the improper translation of the notes.


[1] The name of the naming type and method of the named specification using Pascal



[2] refers to the use of camel naming conventions to name local variables and method parameters

[3] Hungarian nomenclature: Variable name = variable type + variable of English meaning (or abbreviation)

[4] namespace is used to declare a range. This namespace scope allows you to organize your code and gives you the means to create a globally unique type.

[5] The delegate declaration defines a reference type that can be used to encapsulate a method with a specific signature. A delegate instance encapsulates a static method or an instance method. A delegate is roughly analogous to a function pointer in C + +, but a delegate is type safe and reliable

[6] Partial simply by using an incomplete class definition and assigning a portion to each file



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.