C # Programming specification

Source: Internet
Author: User
Programming | Specification C # coding rules

First, naming

1. Use Pascal rules to name methods and types.

public class TextBox

{

public void DataBind ()

{

}

}

2. Use the camel rule to name the parameters of local variables and methods.

String UserName;

Public AddUser (String userId, byte[] password);

3. Prefix all the member variables _

public class Database

{

private string _connectionstring;

}

4. The name of the interface plus prefix I.

Interface Icompare

{

int compare ();

}

5. Custom attributes End With attribute

public class Authorattribute:attribute

{

}

6. Custom exceptions End With exception

public class Appexception:exception

{

}

7. Name of the method. It is generally named as the verb-object phrase.

ShowDialog ()

CreateFile ()

GetPath ()

8. Code indentation. Use tab instead of space.

9. The name of the local variable should be meaningful. Do not use x,y,z and so on (except for the use of i,j,k,l,m,n in the For loop variable).

String UserName

10. All member variables are declared at the top of the class, separating it from the method with a newline.

11. Name namespace with meaningful names, such as: Product name, company name.

12. It is recommended that local variables be declared when they are closest to use.

13. When using the value of a control, try to name the local variable.

14. Separate the namespace and customizations of the referenced system or the third party with a newline.

15. File name to be able to reflect the content of the class, preferably with the class name, a file in a class or a group of connected classes.

16. The structure of the directory to reflect the level of namespace.

17. Brace "{" To start a new line.

public class Authorattribute:attribute

{

}

Second, coding habits.

1. Use C # predefined class names, not aliases.

String UserName; rather than System.String userName;

int number; rather than System.Int32;

2. Do not exceed 80 characters in one line.

3. Try not to manually change the machine generated code, if you have to change, must be changed and machine generated code style.

4. Critical statements, including declaring critical variables, must be written to comment.

5. Literal constants and numeric constants are not hard-coded and should be replaced with constant classes or enumerations.

6. Do not use Goto series statements.

7. Do not declare public and protected member variables, apply property.

8. Do not declare public events, apply event accessors.

public class Source

{

Private EventHandler m_numberchangeevent;

Public event EventHandler Numberchangeevent

{

Add

{

M_numberchangeevent + = value;

}

Remove

{

M_numberchangeevent = value;

}

}

}

9. Usage rules for type conversions.

Animal Animal = new Dog ();

Dog Dog = animal as Dog;

if (dog!= null)

{

}

10. When generating and building a long string, be sure to use StringBuilder instead of string.

11. Always use "{}" to contain statements under if, even if there is only one statement.

The 12.switch statement must have default to handle unexpected situations.

13. Use the three-mesh operator as little as possible? :, but to use the IF statement.

14. Try not to use this reference unless you want to invoke another constructor in the class.

public class Person

{

Public person (string name)

{

}

Public person (): This ("Jim")

{

}

}




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.