C # naming rules and development habits

xiaoxiao2021-03-06  176

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 member variables prefix M_

Public Class Database

{

Public string m_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 must be meaningful. Do not use x, y, z, etc.

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 the Namespace from the referenced system and custom or third parties.

15. The file name is to react the content, it is best to be a class with the same name, a class 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. Don't use the GOTO family of statements.

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 accidents .13. Try not to use the three-mean operator?:, And use 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")

{

}

}

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

New Post(0)