if (shepherd != null )
{…}
38. 当类成员包括委托的时候
a) Copy a delegate to a local variable before publishing to avoid concurrency race
condition.
b) 在调用委托之前一定要检查它是否为null
public class MySource
{
public event EventHandler MyEvent;
public void FireEvent()
{
EventHandler temp = MyEvent;
if(temp != null )
{
temp(this,EventArgs.Empty);
}
}
}
39. 不要提供公共的事件成员变量,使用事件访问器替换这些变量。
public class MySource
{
MyDelegate m_SomeEvent ;
public event MyDelegate SomeEvent
{
add
{
m_SomeEvent += value;
}
remove
{
m_SomeEvent -= value;
}
}
}
40. 使用一个事件帮助类来公布事件的定义。
41. 总是使用接口。
42. 类和接口中的方法和属性至少为2:1的比例。
43. 避免一个接口中只有一个成员。
44. 尽量使每个接口中包含3-5个成员。
45. 接口中的成员不应该超过20个。
a) 实际情况可能限制为12个
46. 避免接口成员中包含事件。
47. 避免使用抽象方法而使用接口替换。
48. 在类层次中显示接口。
49. 推荐使用显式的接口实现。
50. 从不假设一个类型兼容一个接口。Defensively query for that interface.
SomeType obj1;
IMyInterface obj2;
obj2 = obj1 as IMyInterface;
if (obj2 != null)
{
obj2.Method1();
}
else
{
//处理错误
}
51. 表现给最终用户的字符串不要使用硬编码而要使用资源文件替换之。








