技术开发 频道

《Effective C# 精髓》摘选

【IT168 技术文档】

  昨天买了一本《Effective C#》,看了几个Item,虽然没有当初读《Effective C++》时的那般震撼,但是也收获不少。把其中的要点记录于下,有些条款加上了自己的理解,权当作读书笔记。
 
Item 1: Always Use Properties Instead of Accessible Data Members
 
  这个是地球人都知道的条款了。你需要记住,属性是类的外部接口部分,而(公共)成员却是内部实现。如果把内部实现暴露给外部,对于以后类的实现变更是非常不利的。
 
Item 2: Prefer readonly to const
 
  这个条款需要注意一下几点:
 
  (1)const在编译期发生作用,即编译器会将所有的const成员置换成对应的常量
 
  (2)即使引用其他程序集中的const成员,本程序集中也是硬编码了const成员的值。
 
  (3)readonly在运行期被评估,所以其性能比const稍差,但是灵活性更高。
 
  (4)const的值必须在编译期决定,所以不能使用new为其赋值。
 
  (5)更新一个公有的const成员的值应被视为接口改变,而更新一个readonly变量的值可视为内部实现的改变。
 
Item 3: Prefer the is or as Operators to Casts
 
  (1)is或as称为动态转换,是尝试性的,如果失败,不会抛出异常。尽可能使用as操作符。该机制使用元数据完成功能。
 
  (2)Cast称为强制转换,如果失败,则抛出异常--代价高昂。
 
  (3)is、as、Cast转换都不会调用自定义的转换操作符。
 
  (4)is可以判断一个object是否为值类型,而as不行。
 
  (5)请注意Type.IsAssignableFrom()和Type.IsSubclassOf()方法,他们也是常用的类型检测手段。注意,Type.IsSubclassOf()方法不支持接口检测,而Type.IsAssignableFrom()支持。
 
Item 4: Use Conditional Attributes Instead of #if
 
  使用#if常(可能)导致性能问题(如空方法调用)和程序对#if/#endif块代码的依赖问题。
 
  (1)使用Conditional Attributes修饰的方法总是会被编译到目标程序集中,无论是Release或Debug。
 
  (2)如果条件不满足该Conditional Attributes指定的条件,则编译器会忽略所有对其修饰的方法的调用。
 
  (3)被Conditional Attributes修饰的方法必须返回void,这是有道理的。因为我们的程序运行不能依赖被Conditional Attributes修饰的方法的返回值。否则,在不同的条件下,我们的程序将表现出非我们期望的不用行为。
 
Item 5: Always Provide ToString()

  关于这一点,我在以往的项目中早有体会。举个例子,曾经我们需要把从数据库中取出的Customer列表绑定到ComboBox,开始时我们设计Customer时并没有重写ToString()方法,所以我们要这样做:

//从数据库中挑出所有有效用户 string whereStr = string.Format   ("where {0} = '1'" ,Customer._IsValid) ; Customer[] customers = (Customer[])DataEntrance.   GetObjects(typeof(Customer) ,whereStr) ; ArrayList cusNameList = new ArrayList() ; foreach(Customer cus in customers) { cusNameList.Add(string.Format("{0} {1}" ,cus.ID ,cus.Name)) ; } //绑定 this.comboBox1.DataSource = cusNameList;
  如果为Customer重写ToString()方法:
#region ToString public override string ToString() { return this.ID.ToString() + " " + this.Name.ToString() ; } #endregion
  则只需要这样:
string whereStr = string.Format   ("where {0} = '1'" ,Customer._IsValid) ; Customer[] customers = (Customer[])DataEntrance.   GetObjects(typeof(Customer) ,whereStr) ; this.comboBox1.DataSource = customers ;
  这样就简便了好多,而且这样做还有一个好处,比如,从ComboBox从选取一个客户时,以前需要这样:
string cusID=this.comboBox1.SelectedItem.ToString().Split('')[0]; Customer desCus = null ; foreach(Customer cus in customers) { if(cus.ID = cusID) { desCus = cus ; break ; } }
  现在,简单多了,一行代码搞定:
Customer desCus = this.comboBox1.SelectedItem as Customer ;

0
相关文章