C # project development specifications

Source: Internet
Author: User

Ideographic naming rules are the core of program planning. If all the names in the system are suitable for their functions, they can make people "look at their minds", such as "Age" and "SetAge () ", which can greatly improve the maintainability and robustness of the system and make the system relationship clear. Otherwise, if the name cannot reflect its meaning, such as "v001" and "f002 ()", it will be counterproductive.

Description

The naming conventions are as follows.

(1) Pascal style: contains one or more words. The first letter of each word is in upper case, other letters are in lower case, and other letters are in lower case. For example, CollegeStudent and HelloWorld.

(2) Camel style: contains one or more words. The first letter is lowercase, And the other letters are lowercase. For example, name, gender, and somePara.

Below are some common naming rules for your reference in development.

1. namespace

The namespace adopts the Pascal style. The naming rules are as follows.

CompanyName. policyname

For example:

Microsoft. Office

MyCompany. NamingRule. Test

In addition, the name of the complex namespace must be used when the complex number is required. For example, use System. Collections instead of System. Collection. However, when you encounter abbreviations, you do not need to use the plural. For example, use System. IO instead of System. IOs.

The same name cannot be used for namespaces and classes. For example, after a class is named Student, Student is no longer used as a namespace.

2. Class

Class naming in C # adopts the Pascal naming style. The naming rules are as follows.

(1) Before naming a class, you must first know the role of the class and try to name it by noun or noun phrase so that the programmer can provide clues Through the class name, you can understand the basic functions of this class.

(2) Try not to use abbreviations, but to use full writing. For example, use CollegeStudent instead of CollegeStu.

(3) do not use any Class prefix (such as C) or suffix (such as Class ).

(4) do not use underlined characters (for example, College_Student ).

The following is an example of a reasonable class name.

Code 19-1 class naming example

/// <Summary>

/// Class Name: Pascal naming style, such as SomeClass.

/// </Summary>

Pulibc class CollegeStudent

{

...

}

3. Private member

Class member variables adopt the Camel style and use the prefix m _ or _. Below are some reasonable examples of private members.

Code 19-2 Private member naming example

Class CollegeStudent

{

/// <Summary>

/// Private member naming: Camel naming style, such as member.

/// </Summary>

Private string m_name;

Private int m_age;

}

In addition, some programmers are used to using the Data Type prefix to determine the Data Type of the parameter. Such as strName and nAge, but this is not a general specification.

4. Attributes

Class attributes adopt the Pascal style. The following are some reasonable attribute examples.

Code 19-3 property naming example

Class CollegeStudent

{

/// <Summary>

/// Attribute Name: Pascal naming style, such as Name.

/// </Summary>

Public string Name

{

Set

{

If (value! = Null)

This. m_name = value;

}

Get

{

Return this. m_name;

}

}

}

5. Method

Generally, each method is an "action" of the execution class. Therefore, the method name should clearly indicate what the method is, the "Verb + noun" structure can clearly express this meaning. For example, if ShowInfo () is used instead of Info () and LoadData () is used instead of DataLoad (), the purpose of this method is to clarify the function of this method.

Below are some reasonable examples of method names.

Code 19-4 method naming example

Class CollegeStudent

{

/// <Summary>

/// Method Name: Pascal naming style, such as SomeMethod.

/// </Summary>

Public void EnterSchool (){...}

}

In addition, some prefixes are often used to express the meaning of the method, as shown below.

(1) Is refers to a question about something. For example, IsMale ().

(2) Get indicates obtaining a value. For example, GetInfo ().

(3) The meaning of Set is to Set a value. For example, SetInfo ().

6. method parameters

In C #, the parameters of the method adopt the camel style. In addition, some programmers are used to using the Data Type prefix to determine the Data Type of the parameter. Such as strName and nAge.

The following are examples of method parameter naming.

Code 19-5 method parameter naming example

Class CollegeStudent

{

Public void SetInfomation (string name, int age ){...}

}

7. Interface

Similar to the method, interfaces use Pascal naming rules. The naming rules are as follows.

(1) Using I as the prefix indicates that it is an interface.

(2) Use a noun or noun phrase or an adjective describing a behavior to name an interface. For example, IComponent, ICustomAttributeProvider, and IPersistable ).

(3) Try not to use abbreviations, but to use full writing. For example, use IComponent instead of IComp.

(4) do not use underlined characters (for example, ICustom_AttributeProvider ).

For example:

Code 19-6 interface naming example

Class CollegeStudent

{

/// <Summary>

/// Interface Name: Pascal naming style, such as ISomeInterface.

/// </Summary>

Public interface IPlay {};

}

8. Variables

Local variables adopt the camel style, and use descriptive nouns or noun phrases whenever possible, without abbreviations, such as number, rather than num. The following are examples of variable naming.

Int number = 0;

String sqlString = "";

Double averageScore = 0.0;

CollegeStudent collegeStudent = new CollegeStudent ();

Related Article

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.