首页 > 开发 > 综合 > 正文

IDesign C#编码规范(之三)

2024-07-21 02:19:28
字体:
来源:转载
供稿:网友

续之二,idesign c#编码规范之三。

34. 避免使用new继承修饰符,而是使用override。
avoid using the new inheritance qualifier. use override instead.
35. 对非密封类总是将public和protected方法标记为virtual。
always mark public and protected methods as virtual in a non sealed class.
36. 除非涉及到互操作,永远不要用不安全的代码。
never use unsafe code unless when using interop.
37. 避免显式类型转换。使用as算法防护性地转换类型。
avoid explicit casting. use the as operator to defensively cast to a type.
dog dog = new germanshepherd();
germanshepherd shepherd = dog as germanshepherd;
if(shepherd != null)
{...}
38. 类成员有委托时:
with delegates as class members:
a) 使用前将委托复制到局部变量,以避免并发冲突。
copy a delegate to a local variable before publishing to avoid concurrency race condition.
b) 调用前始终检查委托是否为空。
always check a delegate for null before invoking it.
public class mysource
{
public event eventhandler myevent;
public void fireevent()
{
eventhandler temp = myevent;
if(temp != null)
{
temp(this,eventargs.empty);
}
}
}
39. 不要提供public的事件成员变量,而是使用事件访问器。
do not provide public event member variables. use event accessors instead.
public class mysource
{
mydelegate m_someevent;
public event mydelegate someevent
{
add
{
m_someevent += value;
}
remove
{
m_someevent -= value;
}
}
}
40. 使用programming .net components中定义的eventshelper类安全地发布事件。
use the eventshelper class defined in programming .net components to publish events defensively.
41. 总是使用接口。
always use interfaces.
a) 参见programming .net components第一和第三章。
see chapters 1 and 3 in programming .net components.
42. 类和接口中方法和属性的比例至少是2:1。
classes and interfaces should have at least 2:1 ratio of methods to properties.
43. 避免使用一个成员的接口。
avoid interfaces with one member.
44. 努力使每个接口拥有3-5个成员。
strive to have 3-5 members per interface.
45. 每个接口不用超过20个成员。
no more than 20 members per interface.
a) 12可能是实际应用的极限了。
12 is probably a practical limit.
46. 避免将事件作为接口成员。
avoid events as interface members.
47. 避免使用抽象方法,而是使用接口代替。
avoid abstract methods, use interfaces instead.
48. 在类层次中暴露接口。
expose interfaces on class hierarchies.
a) 参见programming .net components第三章。
see chapter 3 in programming .net components.
49. 优先使用明确的接口实现。
prefer using explicit interface implementation.
a) 参见programming .net components第三章。
see chapter 3 in programming .net components.
50. 永远不要假设一种类型支持某个接口。防护性地检查是否支持该接口。
never assume a type supports an interface. defensively query for that interface.
sometype obj1;
imyinterface obj2;

/* some code to initialize obj1, then: */
obj2 = obj1 as imyinterface;
if(obj2 != null)
{
obj2.method1();
}
else
{
//handle error in expected interface
}
51. 将呈现给用户的字符串永远不用硬编码,而是使用资源。
never hardcode strings that will be presented to end users. use resources instead.
52. 发布时可能修改的字符串永远不用硬编码,例如连接字符串。
never hardcode strings that might change based on deployment such as connection strings.
53. 构建一个长字符串时,使用stringbuilder,不要用string。
when building a long string, use stringbuilder, not string.
54. 避免提供带结构的方法。
avoid providing methods on structures.
a) 参数化的构造函数是鼓励使用的。
parameterized constructors are encouraged.
b) 可以重载算符。
can overload operators.
55. 当提供静态成员变量时,总是提供一个静态构造函数。
always provide a static constructor when providing static member variables.
56. 只要可以用前期绑定就不要用后期绑定。
do not use late-binding invocation when early-binding is possible.
57. 对应用程序进行日志和跟踪。
use application logging and tracing.
58. 除非在switch语句中跳转,永远不要用goto语句。
never use goto unless in a switch statement fall-through.
59. switch语句中总是使用default用于加断言。
always have a default case in a switch statement that asserts .
int number = somemethod();
switch(number)
{
case 1:
trace.writeline("case 1:");
break;
case 2:
trace.writeline("case 2:");
break;
default:
debug.assert(false);
break;
}
60. 除非在构造函数中调用另一个构造函数,否则不用使用this。
do not use the this reference unless invoking another constructor from within a constructor.
//example of proper use of this
public class myclass
{
public myclass(string message)
{}
public myclass() : this("hello")
{}
}
61. 除非为了解决调用基类构造函数时成员名的冲突,否则不要使用base访问基类的成员。
do not use the base word to access base class members unless you wish to resolve a conflict with a subclasses member of the same name or when invoking a base class constructor.
//example of proper use of 抌ase?
public class dog
{
public dog(string name)
{}
virtual public void bark(int howlong)
{}
}
public class germanshepherd : dog
{
public germanshepherd(string name): base(name)
{}
override public void bark(int howlong)
{
base.bark(howlong);
}
}
62. 根据programming .net components第四章中的模板实现dispose()和finalize()方法。
implement dispose() and finalize() methods based on the template in chapter 4 of programming .net components.
63. 使用泛型的代码中避免与system.object进行类型转换,而是使用限制或as算符。
avoid casting to and from system.object in code that uses generics. use constraints or the as operator instead:
class someclass
{}
//避免 avoid:
class myclass
{
void somemethod(t t)
{
object temp = t;
someclass obj = (someclass)temp;
}
}
//正确 correct:
class myclass where t : someclass
{
void somemethod(t t)
{
someclass obj = t;
}
}
64. 泛型接口不要定义限制。接口层的限制通常能用强类型代替。
do not define constraints in generic interfaces. interface level-constraint can often be replaced by strong-typing.
public class customer
{...}
//避免 avoid:
public interface ilist where t : customer
{...}
//正确 correct:
public interface icustomerlist : ilist
{...}
65. 不要在接口中定义与方法相关的限制。
do not define method-specific constraints in interfaces.
66. 在数据结构中总是优先使用c#泛型。
always prefer using c# generics in data structures.

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表