Note "C # Coding Standard" Chapter ONE

xiaoxiao2021-03-06  59

1 Name Rules and Style

In previous versions of Visual Studio, Microsoft has suggested using Hungarian nomenclature to write code. In recently released .NET and its programming language, Microsoft replaced his rule.

1. Use the Pascal style with the method name [1]

Public Class SomeClass

{

PUBLIC SOMEMETHOD () {}

}

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

Int number;

Void mymethod (int Somenumber)

{}

3. Use i as an interface prefix

Interface ImyInterface

{..}

4. Use m_ as a private member variable prefix

Public Class SomeClass

{

PRIVATE INT M_NUMBER;

}

5. Using Attribute as a suffix of the custom property class

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

7. Use the verbs form name method name, such as showdialog ()

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

9. Use the variable name that is easy to describe

l Avoid using a single character to describe the variable name, such as i or t. You should use Index or Temp to replace

l Avoid using Hungarian nomenclas [3]

l Never use an abbreviated character, such as using NUM instead of Number

10. Always use the C # predefined format, not the alias in the System space

Object not 0bject

String Not String

Int Not Int32

11. Generally, use uppercase letters for format, use Type suffix when processing .NET format Type

// Correct:

Public Class LinkedList

{..}

// Avoid:

Public Class LinkedList

{..}

12. Using an easy-to-understand namespace [4] name, such as product or company name

13. Avoid using a full qualified name of the namespace, use using declaration replacement

14. Avoid the USING statement to place in the namespace

15. Name the spatial grouping of all frameworks, place the user or third party namespace under

Using system;

Using system.collections;

Using system.componentmodel;

Using system.data;

Using mycompany;

Using myControls;

16. Use the delegation reference [5] instead of explicit entrustment instance

Delegate void somedlegate ();

Public void someMethod ()

{..}

Somedlegate somedlegate = someMethod;

17. Maintain strict indentation

l Use 3 empty glitters to indentation

l Never use Tab or not standard indentation, such as 1, 2 or 4 hollow

18. Take the annotation to shrub at the same level with the code.

19. All comments should be inspected by spelling. Note about the missed annotation will indicate redundant development

20. All member variables should be declared at the beginning and separate attributes and method areas using separate lines.

Public Class Myclass

{

INT m_Number;

String m_name;

Public void SomeMethodl ()

{}

Public void someMethod2 ()

{}

}

21. Declare the local variable in places that are as close as possible from the first time using variables

22. The file name should reflect the class contained.

23. When using the Partial class [6] and assigns it to each file part, name the suffix P and attached to each file with an additional number // in myclassp1.cs

Public Partial Class Myclass

{..}

// in myclassp2.cs

Public Partial Class Myclass

{..}

24. Place the striking ({) with new lines

25. For anonymous methods, reference is given to regular code plan, and its indentation should be aligned with anonymous entrusted statement.

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 for anonymous default parameters, you should use knocked.

Delegate void somedlegate ();

// Correct

Somedlegate Somedelegate1 = delegate ()

{

Messagebox.show ("Hello");

}

// Avoid

Somedelegate Somedelegate1 = delegate

{

Messagebox.show ("Hello");

}

Since I have limited essay, the translation notes are improper, please specify. Later, the second part - encoding practice, but take time, because there will be more comments (my habits)

[1] Specially refer to the name and method of naming specification name and method using Pascal

[2] Specific parameters for naming local variables and methods using the Camel naming specification

[3] Hungarian naming rules: variable name = variable type variable English meaning (or abbreviation)

[4] Namespace is used to declare a range. This namespace range allows you to organize code and provide you with a gestational unique type.

[5] Delegate declaration defines a reference type, which can be used to encapsulate methods with a specific signature. Entrusted instance packaging static methods or examples. Entrusted roughly similar to function pointers in C ; however, delegates are type security and reliable of

[6] Partial is simple to use incomplete class definition and assign some of them into each file.

转载请注明原文地址:https://www.9cbs.com/read-112518.html

New Post(0)