Summary of common C naming rules

  • 2020-11-26 18:57:56
  • OfStack

This article provides a detailed summary of common naming rules for C#. Share to everybody for everybody reference. The details are as follows:

Pascal rules
Capitalize the first letter of each word (e.g. TestCounter).

Camel rules
Capitalize the beginning of words other than the first word, e.g. testCounter.

Upper rules
Abbreviations used only for 1 two-character constants, longer than three characters should apply the Pascal rule.

Such as:

public class Math
{
public const PI = ...
public const E = ...
public const FeigenBaumNumber = ...
}


The specific rules are summarized as follows:

Class naming guidance

- Class names should be nouns and noun phrases. Use full words whenever possible.
- Use the Pascal rule
- Do not use class prefixes - Do not use the underscore character (_).
- Sometimes you need to provide a class name that starts with the letter I, although the class is not an interface. This is appropriate as long as I is the first letter of the entire word as part of the class name. For example, the class name IdentityStore is appropriate.
- Where appropriate, use compound words to name derived classes. The second part of the derived class name should be the name of the base class. For example, ApplicationException is an appropriate name for a class derived from a class named Exception because ApplicationException is an Exception. Please exercise reasonable judgment when applying this rule. For example, Button is an appropriate name for a class derived from Control. Although the button is a control, having Control as part 1 of the class name unnecessarily lengthens the name.

Interface naming rules

- Interface names should be nouns and noun phrases or adjectives that describe their behavior, using full words whenever possible. (Example IComponent or IEnumberable)
- Use the Pascal rule
- Prefix the character I with an uppercase letter (that is, the first uppercase letter of the interface name)
Such as:

interface ICompare
{
int Compare();
}


Enumeration naming rule

- Use Pascal case for Enum type and value names.
- Use fewer abbreviations.
- Do not use the Enum suffix on the Enum type name.
- Use a singular name for most Enum types, but a plural name for Enum types as bitfields.
- Always add FlagsAttribute to the domain Enum type in place.


Variable naming

- Use counter variables i, j, k, l, m, n in simple loop statements
- Use the Camel naming convention

Method named

- Use the Pascal rule
- The name of the other party USES the verb/object or object/verb sequence of 1. For example, when verbs come first, names such as InsertWidget and InsertSprocket are used; When you put the object first, you use names such as WidgetInsert and SprocketInsert.
- Recommended names should be verbs or phrasal verbs. For example, Save, SaveCustomer, instead of CustomerSave
- Do not repeat the name of a class in a method. For example, if a class is already named Book, instead of calling a method Book.CloseBook, you can name the method Book.Close.

Attribute name

- Names should be nouns and noun phrases
- Use the Pascal rule
- Use Is (is) as the prefix for bool attributes or variables, and do not use the Flag suffix. For example, IsDeleted should be used instead of DeleteFlag

Collection named

- Names should be nouns and noun phrases
- Use the Pascal rule
- Append "Collection" after the name

Event named

The -ES131en handlers name USES the EventHandler suffix.
- The two parameters are sender and e, respectively
- Use the Pascal rule
- Event parameters use the EventArgs suffix
- Event naming USES syntactic tenses to reflect the state of its excitation, such as Changed, Changing.
- Consider using verb naming. Variable naming
- Use counter variables i, j, k, l, m, n in simple loop statements
- Use the Camel naming convention

Custom attributes end with Attribute

public class AuthorAttribute : Attribute
{
}


Custom exceptions end with Exception
public class AppException : Exception
{
}


Other common coding rules

Indentation of code. Use Tab, not space.
The names of local variables make sense. Do not use x, y, z, etc. (use i, j, k, l, m, n in addition to the loop variables for For).
All member variables are declared at the top of the class, separated from the method by a new line.
Name namespace with meaningful names, such as product name, company name.
Always use "{}" to contain statements under if, even if there are only 1 statement.
Put similar things like data members, properties, methods, events, etc., in 1 place and use #region... # endregion.

The summary of the naming conventions is shown in the table below:

Related to classes:

标识符

大小写

示例

类/结构

Pascal

AppDomain

枚举类型

Pascal

ErrorLevel

枚举值

Pascal

FatalError

事件

 

Pascal

 

ValueChange

异常类

Pascal

WebException

注意 总是以 Exception 后缀结尾。

只读的静态字段

Pascal

RedValue

接口

Pascal

IDisposable

注意 总是以 I 前缀开始。

集合

Pascal

CustomerCollection 注意 总是以Collection结束

方法

Pascal

ToString

命名空间

Pascal

System.Drawing

参数

Camel

typeName

属性

Pascal

BackColor

受保护的实例字段

Camel

redValue

注意 很少使用。属性优于使用受保护的实例字段。

公共实例字段

Pascal

RedValue

注意 很少使用。属性优于使用公共实例字段。

Variable and method parameter naming: Variable descriptions are capitalized with prefix + initial for different data types

Related to variable naming

类型

前缀

示例

Array

arr

arrShoppingList

Boolean

bln

blnIsPostBack

Byte

byt

bytPixelValue

Char

chr

chrDelimiter

DateTime

dtm

dtmStartDate

Decimal

dec

decAverageHeight

Double

dbl

dblSizeofUniverse

Integer

int

intRowCounter

Long

lng

lngBillGatesIncome

Object

obj

objReturnValue

Short

shr

shrAverage

Single

sng

sngMaximum

String

str

strFirstName


Associated with ADO. NET

数据类型

数据类型简写

标准命名举例

Connection

con

conNorthwind

Command

cmd

cmdReturnProducts

Parameter

parm

parmProductID

DataAdapter

dad

dadProducts

DataReader

dtr

dtrProducts

DataSet

dst

dstNorthWind

DataTable

dtbl

dtblProduct

DataRow

drow

drowRow98