Basic conventions for C # naming

Source: Internet
Author: User
Tags comparison table

To use the complete English descriptor that can accurately describe the variable/field/class, such as FirstName. For some of the obvious variables can be a simple name, such as in the cycle of the increment (subtract) variable can be named "I".

Use the terminology of the areas covered by the project to the fullest extent possible.

To use a case-by-case blend to improve the readability of the name. To distinguish multiple words in an identifier, capitalize the first letter of each word in the identifier. Does not use the underline as the separator character. There are two appropriate writing methods that fit into different types of identifiers:

Pasalcasing: The letter capitalization of the first word of the identifier;

CamelCasing: The letter of the first word of the identifier is lowercase.

The following table describes the casing rules for different types of identifiers:

identifiers

Case

Sample

Name space

Pascal

Namespace Com.Techstar.ProductionCenter

Type

Pascal

public class Devslist

Interface

Pascal

public interface Itablemodel

Method

Pascal

public void UpdateData ()

Property

Pascal

public int length{...}

Event

Pascal

public event EventHandler Changed;

Private field

Camel

private string FieldName;

Non-private field

Pascal

public string FieldName;

enumeration value

Pascal

Filemode{append}

Parameters

Camel

public void UpdateData (string fieldName)

Local variables

Camel

String FieldName;

Avoid using abbreviations, and use them sparingly if you must. Also, you should keep a list of the standard abbreviations and keep them consistent when you use them.

For common acronyms, two-letter abbreviations should be in a uniform case (example: iostream,getiostream); acronyms are capitalized and other letters are lowercase (examples: gethtmltag);

Avoid using long names (preferably no more than 15 letters).

Avoid using names that are similar or differ only in size.

6.2. Naming conventions for various types of identifiers

6.2.1. Assembly naming

Company domain name (techstar) + project name + module name (optional), for example:

Central system assembly: techstar.productioncenter;

Central system business logic assembly: Techstar. productioncenter.business;

6.2.2. Namespace naming

Take the same way as assembly naming: Company domain name (techstar) + project name + module name. In addition, it is generally recommended that namespaces and directory structures are the same. For example:

Central system: techstar.productioncenter;

The user control under the central system: Techstar.ProductionCenter.UserControl;

Central system business logic: Techstar. productioncenter.business;

Central system data access: Techstar. Productioncenter.data;

6.2.3. Class and interface naming

The name of the class is to be noun;

Avoid using abbreviations for words unless their abbreviations are already well known, such as HTTP.

The name of the interface should begin with the letter I. To ensure that the standard implementation of the interface name only a "I" prefix, such as the IComponent of the standard implementation of the component;

Name of the generic type parameter: Name the descriptive name to be T or start with T, for example:

public class List<t>

public class Myclass<tsession>

For classes in different namespaces of the same project, name avoids duplication. Avoid conflict and confusion when quoting;

6.2.4. Method naming

The first word is usually a verb.

If the method returns the value of a member variable, the method name is typically a get+ member variable name, and if the returned value is a bool variable, it is generally prefixed with the. In addition, if necessary, consider using attributes to replace methods, specific recommendations see section 10.1.2;

If the method modifies the value of a member variable, the method name is generally: Set + member variable name. Ditto, consider using attribute to replace method;

6.2.5. Variable naming

According to the scope of use, the variables in our code are basically the following types, the public variables of the class, the private variables of the class (protected with public), the parameter variable of the method, the local variable used inside the method. The naming conventions for these variables are basically the same, as shown in the identifier case comparison table. The difference is as follows:

The public variables of Class I are named in the usual way without special requirements;

The private variables of class Ii. can be used in two ways: adding "M" prefixes, such as mworkername;

Iii. The parameter variables of the method adopt camalstring, such as Workername;

Iv. local variables within the method adopt camalstring, such as Workername;

Do not use _ or & as the first letter;

Try to use words that are short and meaningful;

Given variable names are typically used only for variables that are short-lived in life. I,j,k,m,n is generally used for integer;c,d,e commonly used characters;s for string

If the variable is a collection, the variable name is in the plural. For example, the number of rows in a table should be named: Rowscount;

Naming components to use Hungarian nomenclature, all prefixes should follow the same component name abbreviation list

6.3. Component name abbreviation list

The basic principle of abbreviation is to take the first letter of each word of the component class name, and if there is only one word, remove the vowel and leave the consonant. Abbreviations are all lowercase.

Component type

Abbreviation

Example

Label

Lbl

Lblnote

Textbox

Txt

Txtname

Button

Btn

Btnok

ImageButton

Ib

IbOK

LinkButton

Lb

Lbjump

HyperLink

Hl

Hljump

DropDownList

Ddl

Ddllist

CheckBox

Cb

Cbchoice

CheckBoxList

Cbl

Cblgroup

RadioButton

Rb

Rbchoice

RadioButtonList

Rbl

Rblgroup

Image

Img

Imgbeauty

Panel

PnL

Pnltree

Treeview

Tv

Tvunit

Webcomtable

Wct

Wctbasic

Imagedatetimeinput

Dti

Dtistart

ComboBox

Cb

Cblist

Myimagebutton

Mib

Mibok

Webcomm.treeview

Tv

Tvunit

Pagebar

Pb

Pbmaster

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.