C#编程语言规范,很重要,很全面

本文详细列举了从FxCop中整理出来的.NET编程规范,包括设计、全球化、互操作性、命名和性能等多个方面,旨在提高代码质量和可维护性。涵盖抽象类构造方法、接口设计、异常处理、类型参数限制、命名约定和性能优化等多个要点。
摘要由CSDN通过智能技术生成
从FxCop归纳出来的一些规范建议
下面是根据FxCop整理的.NET代码编写规范,仅供参考。
一、 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<
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值