资源预览内容
第1页 / 共14页
第2页 / 共14页
第3页 / 共14页
第4页 / 共14页
第5页 / 共14页
第6页 / 共14页
第7页 / 共14页
第8页 / 共14页
第9页 / 共14页
第10页 / 共14页
亲,该文档总共14页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
C#编码规范1命名规则与风格(naming conventions and style)11命名规则111 变量命名规范类型前缀示例Array arrarrShoppingListBooleanblnblnIsPostBackBytebytbytPixelValueCharchrchrDelimiterDateTimedtmdtmStartDateDecimaldecdecAverageHeightDoubledbldblSizeofUniverseInteger intintRowCounterLong lnglngBillGatesIncomeObject objobjReturnValueShortshtshtAverageSingle sngsngMaximumString strstrFirstName112 控件命名规范类型前缀示例AdRotatoradrtadrtTopAdButtonbtnbtnSubmitCalendarcalcalMettingDatesCheckBoxchkchkBlueCheckBoxListchklchklFavColorsCompareValidatorvalcvalcValidAge CustomValidatorvalxvalxDBCheckDataGriddgrddgrdTitlesDataListdlstdlstTitlesDropDownListdropdropCountriesHyperLinklnklnkDetailsImageimgimgAuntBettyImageButtonibtnibtnSubmitLabellbllblResultsLinkButtonlbtnlbtnSubmitListBoxlstlstCountriesPanelpnlpnlForm2PlaceHolderplhplhFormContentsRadioButtonradradFemaleRadioButtonList radlradlGenderRangeValidatorvalgvalgAgeRegularExpressionvalevaleEmail_ValidatorRepeaterrptrptQueryResultsRequiredFieldValidatorvalrvalrFirstNameTabletbltblCountryCodes TableCelltblctblcGermanyTableRowtblrtblrCountryTextBoxtxttxtFirstNameValidationSummaryvalsvalsFormErrorsXMLxmlcxmlcTransformResults113 ADO.NET控件命名规范 类型前缀示例ConnectionconnconnNorthwind CommandcmdcmdReturnProductsParameterparmparmProductIDDataAdapterdadaProductsDataReaderdtrdtrProductsDataSetdsdsNorthWindDataTabledtdtProductDataRowdrdrRow98DataColumndcdcProductIDDataRelationdreldrelMasterDetailDataViewdvwdvwFilteredProducts114 命名规则补充Form frmClass cls/CModule modGroupBoxgrpPictureBox picComboBoxcboTreeViewtvwListView lvwTabControltabDateTimePickerdtpTimer tmrSplitter splProgressBarpbarRichTextBoxrtfImageListimglToolBartlbMenuItemmnuDatedatStructureudtCrystal Report rpt115 事件处理子程序The name of an event-handling subroutine will consist of the ID of the control that rasied the event followed by the type of event being handled. For example, a subroutine named btnSubmit_Click handles the Click event of a Button control named btnSubmit.When a control that raises an event is not assigned an ID, the type of the control is used instead of the ID. For example, the subroutine named Button_Click handles the Click event of a Button control without an ID.12编码风格1 用pascal规则来命名方法和类型.public class TextBoxpublic void DataBind() 2 用camel规则来命名局部变量和方法的参数.string userName;public AddUser(string userId, byte password);3 所有的成员变量前加前缀 _public class Databaseprivate string _connectionString;4 接口的名称加前缀 I.interface ICompareint compare();5 自定义的属性以Attribute结尾public class AuthorAttribute : Attribute6 自定义的异常以Exception结尾public class AppException : Exception7 方法的命名.一般将其命名为动宾短语.ShowDialog()CreateFile()GetPath()8 代码的缩进.要用Tab,而不要用space, 保持严格的缩进:l 对缩进使用3个空格位l 绝不使用不标准的缩进,如1,2或4个空格位9 局部变量的名称要有意义.不要用x,y,z等等(除用于For循环变量中可使用i,j,k,l,m,n).string userName10.建议局部变量在最接近使用它时再声明.11 所有的成员变量声明在类的顶端,用一个换行把它和方法分开.public class MyClass int _intNumber; string _strName; public void SomeMethodl() public void SomeMethod2() 12.用有意义的名字命名namespace,如:产品名、公司名.13.避免使用命名空间的完整限定名称,应使用using声明替代14.避免using声明放置在命名空间内部15.将所有的框架命名空间分组,将用户或第三方命名空间放置在其下using System;using System.Collections;using System.ComponentModel;using System.Data;using MyCompany;using MyControls;16.使用某个控件的值时,尽量命名局部变量.17.文件名要能反应类的内容,最好是和类同名,一个文件中一个类或一组关连类.18.大括号要新起一行.public class AuthorAttribute : Attribute19.用C#预定义的类名,而不要用在System空间内别名.object而不是System.0bjectstring 而不是System.Stringint而不是System.Int3220.一行不要超过80个字符.21.尽量不要手工更改机器生成的代码,若必须更改,一定要改成和机器生成的代码风格一样.22.关键的语句(包括声明关键的变量)必须要写注释.23.将注释缩拍在与代码缩进同等级的位置上24.所有的注释应该通过拼写检查.拼错的注释将预示冗余的开发25.文字常量和数字常量不要硬编码,应该用常量类或枚举代替.26.不准使用goto系列语句.27.不要声明public和protected的成员变量,应用property.28.不要声明public的event,而应使用事件访问器.public class Sourceprivate EventHandler _NumberChangeEvent;public event EventHandler NumberChangeEventadd_NumberChangeEvent += value;remove_NumberChangeEvent -= value;29.类型转换的使用规则.Animal animal = new Dog();Dog dog = animal as Dog;if (dog != null)30.生成和构建一个长的字符串时,一定要使用StringBuilder,而不用string.31.始终使用 包含if下的语句,即使只有一条语句
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号