C#编码命名规则

C#编码命名规则

1.       基本规则

第一个字母必须大写,并且后面的并发连结词的第一个字母均为大写

例:GeneralManager、SmallDictionary、StringUtil

2.       接口命名规则

接口名称前加“I

interface ICompare

{

     int compare();

}

 

            类的命名.

类名

public class TextBox

{

 public void DataBind() 

 {

 }

}

            WebServer的命名.

类名添加ws前缀

public class wsTextBox

{

 public void DataBind() 

 {

 }

}

 

            方法、属性的命名.

使用Pascal大小写形式,一般将其命名为动宾短语.

ShowDialog()

CreateFile()

            变量、参数

使用Camel 大小写形式

例:int totalCount

            常量

全部大写,单词之间以 “_” 分隔

例:USER_PASSWORD

            代码的缩进

用Tab,而不要用space

            其他代码命名规则

l           局部变量的名称要有意义.不要用xyz等等(循环变量除外)

l           所有的成员变量声明在类的顶端,用一个换行把它和方法分开

l           文件名要能反应类的内容,最好是和类同名,一个文件中一个类.

l           大括号"{"要新起一行.

public class AuthorAttribute : Attribute

{

}

l           switch语句一定要有default来处理意外情况

l           同程序外部连接(数据库、接口、文件等)一定要捕获任何类型的异常(try

给出友好的消息给用户,必要时用日志记录错误的细节,包括发生的时间,和相关方法,类名等。不要“捕捉了异常却什么也不做”

l           始终使用"{ }"包含if/else下的语句,即使只有一条语句

l           把引用的系统的namespace和自定义或第三方的分开

l           自定义的属性以Attribute结尾

public class AuthorAttribute : Attribute

{

}

 

l           自定义的异常以Exception结尾

public class AppException : Exception

{

}

l           注释需和代码对齐

l           用一个空行来分开代码的逻辑分组

l           花括弧 ( {} ) 需和括号外的代码对齐

l           不在代码中使用具体的路径和驱动器名,使用相对路径,并使路径可编程

            数据库命名规则

l           表:

e—业务表中相对独立的实体表(例如:课程资源表……

r—业务中产生的关联表(例如:选课表……

b—系统中的基本表(例如:类型表、民族、地区……

l           视图: 

vw_<ViewName>       首字母大写

l           存储过程:

usp_<SpName>          首字母大写

l           触发器:

trg _<TriggerName> 首字母大写

l          函数过程:

ufn_<FunctionName>     首字母大写

l          列名:

一般第一列名为ID、主键、标识递增

其他列名尽可能用英文单词或英文缩写,如英文过长(超过6位)或过于生僻可用汉语拼音的首字母。

            控件命名:

Control type                        prefix             Example

Button                            btn             btnOK

Grid                              grd             grdPrices

3D Panel                          pnl             pnlGroup

ADO Data                          ado             adoBiblio

Animated button                   ani             aniMailBox

Check box                         chk             chkReadOnly

Combo box                         cbo             cboEnglish

Dropdown list box             ≥ ddl             ddlUser

Command button                    cmd             cmdExit

Common dialog                    dlg             dlgFileOpen

Communications                   com             comFax

Control (used within procedures when the specific type is unknown)

ctr             ctrCurrent

Data                              dat             datBiblio

Data-bound combo box              dbcbo           dbcboLanguage

Data-bound grid                   dbgrd           dbgrdQueryResult

Data-bound list box               dblst           dblstJobType

Data combo                        dbc             dbcAuthor

Data grid                         dgd             dgdTitles

Data list                         dbl             dblPublisher

Data repeater                     drp             drpLocation

Date picker                       dtp             dtpPublished

Directory list box                dir             dirSource

Drive list box                    drv             drvTarget

File list box                     fil             filSource

Flat scroll bar                   fsb             fsbMove

Form                              frm             frmEntry

Frame                             fra             fraLanguage

Gauge                             gau             gauStatus

Graph                             gra             graRevenue

Hierarchical flexgrid             flex            flexOrders

Horizontal scroll bar             hsb             hsbVolume

Image                             img             imgIcon

Image combo                       imgcbo          imgcboProduct

ImageList                         ils             ilsAllIcons

Label                             lbl             lblHelpMessage

Lightweight check box             lwchk           lwchkArchive

Lightweight combo box             lwcbo           lwcboGerman

Lightweight command button        lwcmd           lwcmdRemove

Lightweight frame                 lwfra           lwfraSaveOptions

Lightweight horizontal scroll bar lwhsb           lwhsbVolume

Lightweight list box              lwlst           lwlstCostCenters

Lightweight option button         lwopt           lwoptIncomeLevel

Lightweight text box              lwtxt           lwoptStreet

Lightweight vertical scroll bar  lwvsb           lwvsbYear

Line                              lin             linVertical

List box                          lst             lstPolicyCodes

ListView                          lvw             lvwHeadings

MAPI message                      mpm             mpmSentMessage

MAPI session                      mps             mpsSession

MCI                               mci             mciVideo

Menu                              mnu             mnuFileOpen

Month view                        mvw             mvwPeriod

MS Chart                          ch              chSalesbyRegion

MS Flex grid                      msg             msgClients

MS Tab                           mst             mstFirst

OLE container                     ole             oleWorksheet

Option button                     opt             optGender

Picture box                       pic             picVGA

Picture clip                      clp             clpToolbar

ProgressBar                       prg             prgLoadFile

Remote Data                       rd              rdTitles

RichTextBox                       rtf             rtfReport

Shape                             shp             shpCircle

Slider                            sld             sldScale

Spin                              spn             spnPages

StatusBar                         sta             staDateTime

SysInfo                           sys             sysMonitor

TabStrip                          tab             tabOptions

Text box                          txt             txtLastName

Timer                             tmr             tmrAlarm

Toolbar                           tlb             tlbActions

TreeView                          tre             treOrganization

UpDown                            upd             updDirection

Vertical scroll bar               vsb             vsbRate

 

注记

Pascal 大小写形式——所有单词第一个字母大写,其他字母小写。

Camel 大小写形式——除了第一个单词,所有单词第一个字母大写,其他字母小写。

 

download: CSharp_coding_naming_guideline.rar

你可能感兴趣的:(命名规则)