Effective C# 精髓 (待續)

來源:互聯網
上載者:User

    昨天買了一本《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不行。
(6)請注意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 ;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.