C#命名规则和开发习惯

2009年4月27日星期一

C#命名规则和开发习惯

 

一、命名


 


1.pascal规则来命名方法和类型,类名添加cls前缀。


Pascal规则是:第一个字母必须大写,并且后面的并发连结词的第一个字母均为大写,例如象GeneralManager、SmallDictionary、StringUtil都是合乎这种规则的类名。


public class clsTextBox


{


     public void DataBind() 


     {


     }


}



 


2.camel规则来命名局部变量和方法的参数.并添加数据类型前缀。前缀但参考附录)


camel规则是:第一个子串外的所有子串的第一个字母大写


string strUserName;


public AddUser(string strUserId, byte[] byPassword);



 


3.所有的成员变量前加前缀 m_


public class Database


{


     public string m_strConnectionString;


}


 


4.接口的名称加前缀 I.


interface ICompare


{


     int compare();


}


 


5.自定义的属性以Attribute结尾


public class AuthorAttribute : Attribute


{


}


 


6.自定义的异常以Exception结尾


public class AppException : Exception


{


}


 


7.方法的命名.一般将其命名为动宾短语.


ShowDialog()


CreateFile()


GetPath()


 


8.代码的缩进.要用Tab,而不要用space.


 


9.局部变量的名称要有意义.不要用xyz等等.


string userName


 


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


 


11.用有意义的名字命名namespace,如:产品名、公司名.


 


12.建议局部变量在最接近使用它时再声明.


 


13.使用某个控件的值时,尽量命名局部变量.


 


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


 


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


 


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


public class AuthorAttribute : Attribute


{


}


17常量


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


例如:USER_PASSWORD 


 


二、编码习惯.


1.C#预定义的类名,而不要用别名.


string userName;   而不是 System.String userName;


int number;        而不是 System.Int32;


 


2.一行不要超过80个字符.


 


3.尽量不要手工更改机器生成的代码,若必须更改,一定要改成和机器生成的代码风格一样.


 


4.关键的语句(包括声明关键的变量)必须要写注释.


 


5.文字常量和数字常量不要硬编码,应该用常量类或枚举代替.


 


6.不要用goto系列语句.


 


7.不要声明publicprotected的成员变量,应用property.


 


8.不要声明publicevent,应用事件访问器.


public class Source


{


     private EventHandler m_NumberChangeEvent;


    


     public event EventHandler NumberChangeEvent


     {


         add


         {


              m_NumberChangeEvent += value;


         }


        


         remove


         {


              m_NumberChangeEvent -= value;


         }


     }


}


 


9.类型转换的使用规则.


Animal animal = new Dog();


Dog dog = animal as Dog;


if (dog != null)


{


}


 


10.生成和构建一个长的字符串时,一定要使用StringBuilder,而不用string.


 


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


 


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


 



13.尽量不用使用this引用,除非是要调用类中的另一个Constructor.


public class Person


{


     public Person(string name)


     {


     }


    


     public Person() : this("Jim")


     {


     }


}


 


数据库命名规则


 


1、数据库对象命名规则


表:    tb_e|r|b_<TableName>  首字母大写


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


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


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


视图:    vw_<ViewName>       首字母大写


存储过程:sp_<SpName>          首字母大写


触发器:  ti | tu | td_<TriggerName> 首字母大写


函数过程:fn_<FunctionName>     首字母大写


 


3、数据项命名规则


列名命名规则:首字母大写


 


注:建议名称部分用英文单词或单词简写


 


 


附录:


 


数据类型     





























bool-b


float-flt


unit-ui


byte-by


int—i


ulong-ulng


char-ch


long—lng


ushort-usht


decimal-dec


sbyte-sby


string-str


double-dbl


short-sht


object-obj


emun-emn


struct-st


 


 


控件命名:


Control type                        prefix             Example


3D Panel                          pnl             pnlGroup


ADO Data                          ado             adoBiblio


Animated button                   ani             aniMailBox


Button                            btn             btnOK


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


Grid                              grd             grdPrices


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


0 评论:

发表评论