您好,欢迎访问三七文档
C#编码规范1.命名规则与风格(namingconventionsandstyle)1.1命名规则1.1.1变量命名规范类型前缀示例ArrayarrarrShoppingListBooleanblnblnIsPostBackBytebytbytPixelValueCharchrchrDelimiterDateTimedtmdtmStartDateDecimaldecdecAverageHeightDoubledbldblSizeofUniverseIntegerintintRowCounterLonglnglngBillGatesIncomeObjectobjobjReturnValueShortshtshtAverageSinglesngsngMaximumStringstrstrFirstName1.1.2控件命名规范类型前缀示例AdRotatoradrtadrtTopAdButtonbtnbtnSubmitCalendarcalcalMettingDatesCheckBoxchkchkBlueCheckBoxListchklchklFavColorsCompareValidatorvalcvalcValidAgeCustomValidatorvalxvalxDBCheckDataGriddgrddgrdTitlesDataListdlstdlstTitlesDropDownListdropdropCountriesHyperLinklnklnkDetailsImageimgimgAuntBettyImageButtonibtnibtnSubmitLabellbllblResultsLinkButtonlbtnlbtnSubmitListBoxlstlstCountriesPanelpnlpnlForm2PlaceHolderplhplhFormContentsRadioButtonradradFemaleRadioButtonListradlradlGenderRangeValidatorvalgvalgAgeRegularExpressionvalevaleEmail_ValidatorRepeaterrptrptQueryResultsRequiredFieldValidatorvalrvalrFirstNameTabletbltblCountryCodesTableCelltblctblcGermanyTableRowtblrtblrCountryTextBoxtxttxtFirstNameValidationSummaryvalsvalsFormErrorsXMLxmlcxmlcTransformResults1.1.3ADO.NET控件命名规范类型前缀示例ConnectionconnconnNorthwindCommandcmdcmdReturnProductsParameterparmparmProductIDDataAdapterdadaProductsDataReaderdtrdtrProductsDataSetdsdsNorthWindDataTabledtdtProductDataRowdrdrRow98DataColumndcdcProductIDDataRelationdreldrelMasterDetailDataViewdvwdvwFilteredProducts1.1.4命名规则补充FormfrmClasscls/CModulemodGroupBoxgrpPictureBoxpicComboBoxcboTreeViewtvwListViewlvwTabControltabDateTimePickerdtpTimertmrSplittersplProgressBarpbarRichTextBoxrtfImageListimglToolBartlbMenuItemmnuDatedatStructureudtCrystalReportrpt1.1.5事件处理子程序Thenameofanevent-handlingsubroutinewillconsistoftheIDofthecontrolthatrasiedtheeventfollowedbythetypeofeventbeinghandled.Forexample,asubroutinenamedbtnSubmit_ClickhandlestheClickeventofaButtoncontrolnamedbtnSubmit.WhenacontrolthatraisesaneventisnotassignedanID,thetypeofthecontrolisusedinsteadoftheID.Forexample,thesubroutinenamedButton_ClickhandlestheClickeventofaButtoncontrolwithoutanID.1.2.编码风格1用pascal规则来命名方法和类型.publicclassTextBox{publicvoidDataBind(){}}2用camel规则来命名局部变量和方法的参数.stringuserName;publicAddUser(stringuserId,byte[]password);3所有的成员变量前加前缀_publicclassDatabase{privatestring_connectionString;}4接口的名称加前缀I.interfaceICompare{intcompare();}5自定义的属性以Attribute结尾publicclassAuthorAttribute:Attribute{}6自定义的异常以Exception结尾publicclassAppException:Exception{}7方法的命名.一般将其命名为动宾短语.ShowDialog()CreateFile()GetPath()8代码的缩进.要用Tab,而不要用space,保持严格的缩进:对缩进使用3个空格位绝不使用不标准的缩进,如1,2或4个空格位9局部变量的名称要有意义.不要用x,y,z等等(除用于For循环变量中可使用i,j,k,l,m,n).stringuserName10.建议局部变量在最接近使用它时再声明.11所有的成员变量声明在类的顶端,用一个换行把它和方法分开.publicclassMyClass{int_intNumber;string_strName;publicvoidSomeMethodl(){}publicvoidSomeMethod2(){}}12.用有意义的名字命名namespace,如:产品名、公司名.13.避免使用命名空间的完整限定名称,应使用using声明替代14.避免using声明放置在命名空间内部15.将所有的框架命名空间分组,将用户或第三方命名空间放置在其下usingSystem;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.Data;usingMyCompany;usingMyControls;16.使用某个控件的值时,尽量命名局部变量.17.文件名要能反应类的内容,最好是和类同名,一个文件中一个类或一组关连类.18.大括号{要新起一行.publicclassAuthorAttribute:Attribute{}19.用C#预定义的类名,而不要用在System空间内别名.object而不是System.0bjectstring而不是System.Stringint而不是System.Int3220.一行不要超过80个字符.21.尽量不要手工更改机器生成的代码,若必须更改,一定要改成和机器生成的代码风格一样.22.关键的语句(包括声明关键的变量)必须要写注释.23.将注释缩拍在与代码缩进同等级的位置上24.所有的注释应该通过拼写检查.拼错的注释将预示冗余的开发25.文字常量和数字常量不要硬编码,应该用常量类或枚举代替.26.不准使用goto系列语句.27.不要声明public和protected的成员变量,应用property.28.不要声明public的event,而应使用事件访问器.publicclassSource{privateEventHandler_NumberChangeEvent;publiceventEventHandlerNumberChangeEvent{add{_NumberChangeEvent+=value;}remove{_NumberChangeEvent-=value;}}}29.类型转换的使用规则.Animalanimal=newDog();Dogdog=animalasDog;if(dog!=null){}30.生成和构建一个长的字符串时,一定要使用StringBuilder,而不用string.31.始终使用{}包含if下的语句,即使只有一条语句.32.switch语句一定要有default来处理意外情况.33.尽量少使用三目运算符?:,而要使用if语句.34.尽量不用使用this引用,除非是要调用类中的另一个Constructor.publicclassPerson{privatestring_strName;publicPerson(stringname){_strName=name;}publicPerson():this(Jim){}}35.使用易描述的变量名称避免使用单一字符描述变量名称,如i或t.应使用index或temp替代对public以及protected成员避免使用匈牙利命名法则尽量不使用缩写字符,如使用num代替number36.对于泛型,类型使用大写字母,当处理.NET格式Type时使用Type后缀//Correct:publicclassLinkedListK,T{..}//Avoid:publicclassLinkedListKeyType,DataType{..}37.使用委托引用代替显式的委托实例(好像只有C#2.0才能这样用)delegatevoidSomeDelegate();publicvoidSomeMethod(){..}SomeDelegatesomeDelegate=SomeMethod;38.当使用partial类并将其分配给每个文件一部分时,对每个文件命名使用后缀P并附带一个额外的数字//InMyClassP1.cspublicpartialclassMyClass{..}//InMyClassP2.cspublicpartialclassMyClass{..}39.对于匿名方法参照有规律的代码规划,其缩进应与匿名委托声明对齐delegatevoidSomeDelegate(stringsomeString);//Correct:publicvoidInvokeMethod(){SomeDelegatesomeDelegate=delegate(stringname){MessageBox.Show(name);};someDelegate(Juval);}//AvoidpublicvoidInvokeMethod(){SomeDelegatesomeDelegate=delegate(stringname){MessageBox.Show(name);};someDelegate(”Juval);}40.对于匿名缺省参数的方法,应该使用空括号表示.delegatevoidSomeDelegate();//CorrectSomeDelegatesomeDelegate1=delegate(){MessageBox.Show(Hello);};//AvoidSomeDelegatesomeDelegate1=delegate{MessageBox.Show(Hello);};2.编码惯例(codingpractices)1.避免在一
本文标题:C#编码规范1
链接地址:https://www.777doc.com/doc-7028814 .html