NET代码编写规范.doc_第1页
NET代码编写规范.doc_第2页
NET代码编写规范.doc_第3页
NET代码编写规范.doc_第4页
NET代码编写规范.doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

一、 Design(设计)1. Abstract types should not have constructors 抽象类不应该声明构造方法2. Assemblies should have valid strong names 程序集应该具有强名称3. Avoid empty interfaces 避免使用空的接口4. Avoid excessive parameters on generic types 避免在泛型类中使用过多的类型参数5. Avoid namespaces with few types 避免让名字空间含有过少的类型6. Avoid out parameters 避免使用 out类型的参数7. Collections should implement generic interface 集合类应该实现泛型接口8. Consider passing base types as parameters 尽量使用基本类型作为参数9. Declare event handlers correctly 正确的声明事件处理器,事件处理器不应该具有返回值10. Declare types in namespaces 应该在名字空间里面定义类型,而不是外面11. Default parameters should not be used 不应该使用参数默认值(C#没有参数默认值)12. Define accessors for attribute arguments 应该为特性(特性)的构造方法参数定义访问器,其名字跟构造方法参数仅首字母大小写不一样13. Do not catch general exception types 不要捕捉普通的异常(即System.Exception)14. Do not declare protected members in sealed types 不要在封闭类型中定义受保护的成员15. Do not declare static members on generic types 不要在泛型类型中使用静态成员16. Do not declare virtual members in sealed types 不要在封闭类型中定义虚成员17. Do not declare visible instance fields 不要定义可见的(public/internal)实例域变量18. Do not expose generic lists 不要直接暴露范型表19. Do not hide base class methods不要隐藏(使用或者不使用new)基类的方法20. Do not nest generic types in member signatures 不要在成员的签名(参数或者返回值)中嵌套泛型类21. Do not override operator equals on reference types 不要在引用类型中重载=操作符22. Do not pass types by reference 不要使用引用(ref or out)传递类型23. Enum Storage should be Int32 枚举应该是 Int32 类型的24. Enumerators should be strongly typed 枚举器应该是强类型的25. Enums should have zero value枚举应该具有0值26. Generic methods should provide type parameter 泛型类的方法应该提供类型参数27. ICollection implementations have strongly typed members 集合接口的实现中应该使用强类型的成员28. Implement standard exception constructors自定义的异常应该实现异常类的四个标准构造方法29. Indexers should not be multidimensional 索引不应该是多维的30. Interface methods should be callable by child types接口方法应该可以被子类调用31. Lists are strongly typed表应该是强类型的32. Mark assemblies with assembly version用程序集版本标示程序集33. Mark assemblies with CLSCompliant使用CLSCompliant特性标示程序集34. Mark assemblies with ComVisible 使用 System.Runtime.InteropServices.ComVisibleAttribute 特性标示程序集35. Mark attributes with AttributeUsageAttribute使用 AttributeUsageAttribute 特性标示特性类36. Mark enums with FlagsAttribute含有组合的枚举应该使用FlagsAttribute特性标示,相反则不应该37. Members should not expose certain concrete types 成员(返回值或者参数)不应该暴露具体类型,尽量使用接口38. Move pinvokes to native methods class将调用移到本地方法类(不是很理解)39. Nested types should not be visible 嵌套类型不应该是可见的40. Override methods on comparable types可比较类型应该重写 equals 等方法41. Override operator equals on overriding add and subtract 在重写+和-运算的时候应该同时重写=操作符42. Properties should not be write only属性不应该是只写的43. Provide ObsoleteAttribute message 过时的成员应该使用ObsoleteAttribute特性标示,并提供相应的Message提示使用者44. Replace repetitive arguments with params array使用参数数组代替重复的参数45. Static holder types should be sealed 仅含有静态成员的类型应该声明为封闭的46. Static holder types should not have constructors仅含有静态成员的类型应该具有构造方法47. String uri overloads call system uri overloads使用string类型的uri参数的重载应调用系统的使用URI类型参数的重载48. Types should not extend certain base types类型不应该从具体的类(已经过派生的类)继承,比如异常类不应该从ApplicationException继承,而应该从System.Exception继承49. Types that own disposable fields should be disposable 含有可释放成员的类型应该是可以释放的(实现IDisposable接口)50. Types that own native resources should be disposable使用了非托管资源的类型应该是可以释放的(实现IDisposable接口)51. Uri parameters should not be strings Uri 参数不应该是string类型的52. Uri properties should not be strings Uri 属性不应该是string类型的53. Uri return values should not be strings Uri 类型的返回值不应该是string类型的54. Use events where appropriate在适当的时候使用事件55. Use generic event handler instances 使用泛型的事件处理器实例56. Use generics where appropriate 在适当的时候使用范型57. Use integral or string argument for indexers 索引器应该使用整数或者字符串类型的参数58. Use properties where appropriate 在适当的时候使用属性(而不是以Get或者Set开头的方法)59. Validate arguments of public methods对public的方法的参数应该在方法开头处进行检验(比如是否为null的检验)二、 Globalization(全球化)1. Avoid duplicate accelerators 避免在顶层控件中使用重复的快捷键(加速键) 2. Do not hardcode locale specific strings 不要对本地的特殊字符串(比如特殊的系统路径)进行硬编码 3. Do not pass literals as localized parameters 不要把文本作为需要本地化的参数直接传递(尽量使用资源文件)4. Set locale for data types为某些数据类型设定区域和语言属性(DataSet和DataTable的locale属性)5. Specify CultureInfo 指定文化信息(地域和语言信息),在调用接受System.Globalization.CultureInfo 类型参数的方法时应该传递文化信息 6. Specify IFormatProvider 指定格式供应器,在调用接受System.IFormatProvider 类型参数的方法时应该传递格式供应器7. Specify MessageBoxOptions指定MessageBox的选项,在调用MessageBox.Show方法时应该传递System.Windows.Forms.MessageBoxOptions,特别在某些从右向左阅读习惯的区域三、 Interoperability(互操作性)1. Auto layout types should not be ComVisible自动布局的类型不应该对Com可见(设置System.Runtime.InteropServices.ComVisibleAttribute特性为false)2. Avoid int64 arguments for VB6 clients 避免使用int64类型,如果成员可能被Visual Basic 6 COM clients调用 3. Avoid non-public fields in ComVisible value types避免在一个标记有ComVisible特性的值类型里面包含非公有的实例域4. Avoid overloads in ComVisible interfaces 避免在一个标记有ComVisible特性的接口内声明重载5. Avoid static members in ComVisible types 避免在一个标记有ComVisible特性的类型6. Call GetLastError immediately after pinvoke 进行pinvoke以后应该立即使用GetLastError读取错误信息7. Com registration methods should be matched Com注册方法(标记有System.Runtime.InteropServices.ComRegisterFunctionAttribute特性的方法)应该是配对的(同时具有一个标记有System.Runtime.InteropServices.ComUnregisterFunctionAttribute的方法与之匹配)8. Com registration methods should not be visible Com注册方法应该是不可见的9. Com visible type base types should be ComVisible 标记有ComVisible特性的类型的基类同样应从标记有ComVisible特性的类继承10. Com visible types should be creatable 标记有ComVisible特性的类型应该能够使用默认构造器构造11. Declare PInvokes correctly 正确定义PInvokes12. Do not use AutoDual ClassInterfaceType 不要把System.Runtime.InteropServices.ClassInterfaceAttribute特性的值设置为System.Runtime.InteropServices.ClassInterfaceType.AutoDual13. Mark boolean pinvoke arguments with MarshalAs 布尔型的pinvoke参数应该使用System.Runtime.InteropServices.MarshalAsAttribute特性标记14. Mark ComSource interfaces as IDispatch 将System.Runtime.InteropServices.ComSourceInterfacesAttribute特性标记为System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch.15. PInvoke entry points should exist Pinvoke应该存在入口点16. PInvokes should not be visiblePinvoke应该是可见的四、 Naming(命名)1. Avoid language specific type names in parameters 避免在参数中使用与特定语言相关的类型(用Uint16代替Ushort)2. Avoid type names in parameters 避免在外部可见的参数中使用类型名3. Compound words should be cased correctly 复合词应该使用正确的大小写(不要将Mutlipart写成MultiPart,也不要将ToolBar写成Toolbar,FileName写成Filename)4. Do not name enum values Reserved 不要在枚举值中使用保留字5. Do not prefix enum values with type name 不要在枚举值使用类型前缀(比如不要使用Digital之类的前缀)6. Events should not have before or after prefix 事件的名称不应该包含before和after前缀(尽量使用ing和ed的后缀)7. Flags enums should have plural names 标记有System.FlagsAttribute特性的枚举应该使用复数形式的名称8. Identifiers should be cased correctly 标示符(名字空间、类名、属性名、接口名、方法名等)应该使用正确的大小写(通常以大写开头,以后的每个单词都首字母大写)9. Identifiers should be spelled correctly 标示符应该可以被正确的划分为不同的单词10. Identifiers should differ by more than case 标示符应该不止有大小写上的不同(因为某些语言是不区分大小写的)11. Identifiers should have correct prefix 标示符应该使用正确的前缀(接口应该使用字母I开头)12. Identifiers should have correct suffix 标示符应该使用正确的后缀System.Attribute/Attribute System.EventArgs/EventArgs System.Exception/Exception System.Collections.ICollection/Collection System.Collections.IDictionary/Dictionary System.Collections.IEnumerable/Collection System.Collections.Queue/Collection or Queue System.Collections.Stack/Collection or Stack System.Collections.Generic.ICollection/Collection System.Collections.Generic.IDictionary/Dictionary System.Data.DataSet/DataSet System.Data.DataTable/Collection or DataTable System.IO.Stream/Stream System.Security.IPermission/Permission System.Security.Policy.IMembershipCondition/Condition An event-handlerdelegate./EventHandler13. Identifiers should not contain underscores 标示符不应该使用下划线14. Identifiers should not have incorrect prefix 标示符不应该使用不正确的前缀(比如不应使用一个字母作为前缀)15. Identifiers should not have incorrect suffix 标示符不应该使用不正确的后缀(不要在不正确的地方使用12中提及的后缀名,和Delegate、Enum、Flags for an enumeration、Impl等后缀名)16. Identifiers should not match keywords 标示符不应该与系统关键字冲突17. Long acronyms should be pascal-cased 长度大于等于3的缩写词应该使用pascal的命名规则,即首字母大写18. Only FlagsAttribute enums should have plural names 只有标记有System.FlagsAttribute特性的枚举的名称才应该使用复数,其他时候应该使用单数19. Parameter names should match base declaration 派生项的参数名应该同基项相吻合(派生类重写或实现的方法应该同基项具有相同的参数名)20. Parameter names should not match member names 方法的参数名不应该同类或接口的成员名一样21. Property names should not match get methods 属性名字不应该同Get开头的方法的名称的后半部分相同22. Resource string compound words should be cased correctly 包含符合单词的资源字符串应该使用正确的大小写(每个单词的首字母大写)23. Resource strings should be spelled correctly 资源字符串应该正确的拼写24. Short acronyms should be uppercase 短的首字母缩写词应该全部大写(比如DB,CR)25. Type names should not match namespaces类型的名字不应该与名字空间的名字相同26. Use preferred terms优先使用某些项目或者名称,以下这些,后者为优先使用的ComPlus/EnterpriseServices Cancelled/Canceled Indices/Indexes LogIn/LogOn LogOut/LogOff SignOn/SignIn SignOff/SignOut Writeable/Writable五、 Performance(性能规则)1. Avoid calls that require unboxing 避免调用一个方法,它返回object类型,而你需要的是一个值类型(需要对返回值进行拆箱操作)2. Avoid costly calls where possible 尽可能的避免进行代价高昂的调用3. Avoid excessive locals避免使用过多的局部变量(多于64个,部分可能是编译器生成的)4. Avoid uncalled private code避免声明在程序集内从来未被调用的私有成员(private和internal),以下除外:明确的接口成员静态构造方法静态的Main方法(不含参数或仅包含一个string数组的参数的)序列化构造方法标记有System.Runtime.InteropServices.ComRegisterFunctionAttribute或者 System.Runtime.InteropServices.ComUnregisterFunctionAttribute.特性的重写的方法5. Avoid uninstantiated internal classes避免声明不会被实例化的内部类,以下情况除外值类型抽象类型枚举委托编译器生成的数组类型仅含有静态成员的内部类6. Avoid unnecessary string creation 避免创建不必要的string实例(犹指通过ToLower和ToUpper创建的string),含以下情况对于同一个string实例多次调用ToLower和ToUpper(建议:将返回值赋给一个局部变量,然后使用此局部变量)使用equals,=,!=比较通过ToLower和ToUpper创建的string(建议:使用String.Compare比较)向一个System.Collections.Specialized.HybridDictionary类型的成员传递通过ToLower和ToUpper创建的string(建议:HybridDictionary具有一个指示是否忽略大小写的参数的构造方法重载,使用此重载并传递一个true值进去)7. Avoid unsealed attributes 避免声明未封闭的特性(attributes)(建议:声明为sealed/ NotI或者abstract)8. Avoid unused parameters避免在方法声明中包含不会被使用的参数,以下情况除外代理引用的方法作为事件处理程序的方法抽象方法(abstract)虚方法(virtual)重写的方法(override)外部方法(extern)9. Dispose methods should call SuppressFinalizeDispose方法应该调用SuppressFinalize,以请求系统不要调用其Finalize方法10. Do not call properties that clone values in loops 不要在循环中使用返回一个Clone的对象的属性(每次返回引用不同的对象,会导致创建大量的相同的对象)11. Do not cast unnecessar

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论