C # coding rules
First, name
1. Name the methods and types with the PASCAL rules.
Public Class TextBox
{
Public void databind ()
{
}
}
2. Name the parameters of local variables and methods with the Camel rules.
String username;
Public adduser (String Userid, Byte [] Password);
3. All members variables before adding prefixes _
Public Class Database
{
Private string _connectionstring;
}
4. The name of the interface adds prefix I.
Interface iCompare
{
INTCOMPARE ();
}
5. Customized properties end with Attribute
Public Class AuthorAttribute: Attribute
{
}
6. Customized exception ends with the EXCEPTION
Public Class APPEXCEPTION: Exception
{
}
7. Naming method. Generally named by a mobile phrase.
Showdialog ()
Createfile ()
GetPath ()
8. Corridation. To use Tab, not to use Space.
9. The name of the local variable should be meaningful. Do not use X, Y, Z, etc. (I, J, K, L, M, N) can be used in the For FOR cycle variable.
String Username
10. All member variables are declared at the top of the class and separate it from a wrap.
11. Name Namespace with meaningful name, such as: product name, company name.
12. It is recommended that local variables declare when it is closest to it.
13. When using a control value, try to name local variables.
14. Separate them separate from the Namespace and custom or third part of the referenced system.
15. The file name must be able to react the content, it is best to have a class or a set of associations in a file.
16. The title of Namespace is required in the directory structure.
17. Big bracket "{" to do new rows.
Public Class AuthorAttribute: Attribute
{
}
Second, the coding habit.
1. Use the C # predefined class name instead of alias.
String username; not system.string username;
Int number; not system.int32;
2. Do not exceed 80 characters in one line.
3. Try not to manually change the code generated by the machine. If you must change, be sure to change it to the code style generated by the machine.
4. Key statements (including a key variable) must write comments.
5. Text constants and digital constants do not have hard coding, and should be replaced with constant classes or enumerations.
6. Do not use the GOTO series statement.
7. Do not declare the member variables of public and protected, apply Property.
8. Do not declare the EVENT of the public, Apply Event Accessor.
Public Class Source
{
PRIVATE EVENTHANDLER M_NUMBERCHANGEVENT;
Public Event athandler NumberchangeEvent
{
Add
{
m_numberchangeeevent = value;
}
Remove
{
m_numberchangeeevent - = value;
}
}
}
9. Use rules for type conversion.
Animal Animal = New Dog ();
DOG DOG = Animal As Dog;
IF (Dog! = NULL)
{
}
10. When generating and build a long string, you must use StringBuilder without String.
11. Always use the "{}" containing the statements under IF, even if there is only one statement. 12.switch statement must have DEFAULT to handle unexpected situations.
13. Try to use the three-mean operator as possible?:, And use the IF statement.
14. Try not to use this reference, unless you want to call another constructor in the class.
Public Class Person
{
Public Person (String Name)
{
}
Public Person (): this ("jim")
{
}
}
Author blog: http://blog.9cbs.net/ayine/