C#中委託的匿名方法,及其在協變與逆變中的應用

來源:互聯網
上載者:User

 委託的匿名方法
    這幾天在看微軟的MVP設計模式的例子的時候,偶然發現它將DropDownListSelectIndexChange事件委託直接執行個體化成一個不帶任何參數匿名方法。
具體例子如下:

 

Code
    this.customerDropDownList.SelectedIndexChanged += delegate { presenter.DisplayCustomerDetails(); };

    這讓我不禁起了疑惑。SelectedIndexChanged事件委託應該要帶有一個參數EventArgs才對啊。為什麼它用這函數執行個體化呢?幾經查詢,終於明白了,不帶括弧的delegate 關鍵字將定義一種特殊的匿名方法,它可以指派給具有任何簽名的任何委託。就如上面的代碼一樣。如果使用匿名方法,則不必建立單獨的方法,因此減少了執行個體化

委託所需的編碼系統開銷。

 

 委託的協變與逆變

    協變允許方法具有的派生傳回型別比委託中定義的更多。而委託還有一種逆變,逆變允許方法具有的派生參數類型比委託類型中的更少。將方法簽名與委託類型匹配時,協

變和逆變提供了一定程度的靈活性。
*下面分別提供兩個簡單的例子說明一下。
1.協變。

    以下委託將定義為具有Human傳回型別的委託,而執行個體化委託的方法中有一個傳回型別是Women類型的。Women類型是由Human類型派生的。

Code
    class Human
    {
        public override string ToString()
        {
            return "human";
        }
    }
    class Women : Humen
    {
        public override string ToString()
        {
            return "women";
        }
    }

    class Program
    {
        /// <summary> 
        /// 定義一個傳回型別為Human的委託。 
        /// </summary> 
        /// <returns>返回一個Human對象。</returns> 
        public delegate Human GetHumanHandler(bool isWomen);

        public static Human GetHuman(bool isWomen)
        {
            Human human;
            if (isWomen)
            {
                human = new Women();
                Console.WriteLine(human);
                return human;
            }
            human = new Human();
            Console.WriteLine(human);
            return human;
        }

        public static Human GetWomen()
        {
            Women human = new Women();
            Console.WriteLine(human);
            return human;
        }

        static void Main(string[] args)
        {
            //命名方法 
            GetHumanHandler handler1 = GetHuman;
            //匿名方法 
            GetHumanHandler handler2 = delegate
            {
                return GetWomen();
            };
            handler1.Invoke(false);
            handler2.Invoke(true);
        }
    }

2.逆變

    以下委託將定義為一個帶有Human型別參數的委託何一個帶有Women型別參數的委託。Women類型是由Human類型派生的。而執行個體化委託的方法中只
是一個帶有Human型別參數的方法。兩個委託共用一個方法執行個體。

Code
    class Human
    {
    }
    class Women : Human
    {
    }

    class Program
    {
        public delegate void HumanInfoPrintHandler(Human human);
        public delegate void WomenInfoPrintHandler(Women human);

        public static void HumanInfoPrint(Human human)
        {
            if (human is Women)
            {
                Console.WriteLine("women");
            }
            else
            {
                Console.WriteLine("human");
            }
        }

        static void Main(string[] args)
        {
            HumanInfoPrintHandler handler1 = HumanInfoPrint;
            WomenInfoPrintHandler handler2 = HumanInfoPrint;
            handler1.Invoke(new Human());
            handler2.Invoke(new Women());
        }
    }

參考資料:MSDN官方說明文檔(C#編程指南
 http://msdn.microsoft.com/zh-cn/library/ms173174(VS.80).aspx

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.