• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • 《Effective C# 精髓》摘選

    發表于:2007-07-14來源:作者:點擊數: 標簽:
    Item 1: Always Use Properties Instead of Accessible Data Members 這個是地球人都知道的條款了。你需要記住,屬性是類的外部接口部分,而(公共)成員卻是內部實現。如果把內部實現暴露給外部,對于以后類的實現變更是非常不利的。 Item 2: Prefer readon

    Item 1: Always Use Properties Instead of Aclearcase/" target="_blank" >ccessible Data Members

      這個是地球人都知道的條款了。你需要記住,屬性是類的外部接口部分,而(公共)成員卻是內部實現。如果把內部實現暴露給外部,對于以后類的實現變更是非常不利的。
      Item 2: Prefer readonly to const

      這個條款需要注意一下幾點:

     ?。?)const在編譯期發生作用,即編譯器會將所有的const成員置換成對應的常量“值”。

     ?。?)即使引用其他程序集中的const成員,本程序集中也是硬編碼了const成員的值。

     ?。?)readonly在運行期被評估,所以其性能比const稍差,但是靈活性更高。

     ?。?)const的值必須在編譯期決定,所以不能使用new為其賦值。

     ?。?)更新一個公有的const成員的值應被視為接口改變,而更新一個readonly變量的值可視為內部實現的改變。

      Item 3: Prefer the is or as Operators to Casts

     ?。?)is或as稱為“動態轉換”,是嘗試性的,如果失敗,不會拋出異常。盡可能使用as操作符。該機制使用元數據完成功能。

     ?。?)Cast稱為“強制轉換”,如果失敗,則拋出異常--代價高昂。

     ?。?)is、as、Cast轉換都不會調用自定義的轉換操作符。

     ?。?)is可以判斷一個object是否為值類型,而as不行。

     ?。?)請注意Type.IsAssignableFrom()和Type.IsSubclassOf()方法,他們也是常用的“類型檢測”手段。注意,Type.IsSubclassOf()方法不支持接口檢測,而Type.IsAssignableFrom()支持。
    Item 4: Use Conditional Attributes Instead of #if

      使用#if常(可能)導致性能問題(如空方法調用)和程序對#if/#endif塊代碼的依賴問題。

     ?。?)使用Conditional Attributes修飾的方法總是會被編譯到目標程序集中,無論是Release或Debug。

     ?。?)如果條件不滿足該Conditional Attributes指定的條件,則編譯器會忽略所有對其修飾的方法的調用。

     ?。?)被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 ;

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>