C# 委託 事件 匿名方法

來源:互聯網
上載者:User

(*) 委託

從最簡單的例子開始:

namespace ConsoleApplication1
{    
    class Program
    {
        // 委託其實就相當於一個類型。這裡,類型的名字叫BinaryOp
        public delegate int BinaryOp(int x, int y);

        static public int Add(int x, int y)
        {
            return x + y;
        }

        static void Main()
        {
            // 建立一個BinaryOp類型的執行個體,用靜態Add方法初始化
            BinaryOp d = new BinaryOp(Add);

            Console.WriteLine(d(10, 10));
        }
    }
}

上面是靜態方法的委託,下面再來看一個執行個體方法的委託。

class MyClass
{
    private string name;

    public MyClass(string name)
    {
        this.name = name;
    }

    public void DisplayName() 
    {
        Console.WriteLine("{0}", name);
    }
}

class Program
{
    // 委託其實就相當於一個類型。這裡,類型的名字叫SimpleDelegate
    public delegate void SimpleDelegate();        

    static void Main()
    {
        MyClass a = new MyClass("A");
        MyClass b = new MyClass("B");
        
        // 用執行個體方法DisplayName初始化
        SimpleDelegate d = new SimpleDelegate(a.DisplayName);            
        d();

        d = new SimpleDelegate(b.DisplayName);
        d();
    }
}

 

(*) 事件
委託是個類型,而事件是個成員。看下面的代碼:

namespace ConsoleApplication1
{
    public class SimpleMath
    {
        public delegate int BinaryOp(int a, int b); // 定義Binary類型
        public event BinaryOp BinaryEvent; // 定義BinaryEvent成員

        public int Add(int a, int b) { return a + b; }
        public int Substract(int a, int b) { return a - b; }

        public int Calculate() 
        {
            // Raise the event by using the () operator.
            return BinaryEvent(1, 2); // 只能在定義事件的類的內部調用,如果寫在外面會編譯不過
        }
    }

    class Program
    {
        static void Main() 
        {
            SimpleMath sm = new SimpleMath();

            // sm.BinaryEvent(1, 2); 編譯錯誤!只能在定義事件的類的內部調用

            // 下面兩種註冊方法效果是一樣的,相當於註冊了兩遍,也的確會依序執行兩遍
            sm.BinaryEvent += new SimpleMath.BinaryOp(sm.Add);
            sm.BinaryEvent += sm.Add;

            Console.WriteLine(sm.Calculate()); // 結果是3

            // 下面兩種註冊方法效果是一樣的,相當於註冊了兩遍,也的確會依序執行兩遍
            sm.BinaryEvent += new SimpleMath.BinaryOp(sm.Substract);
            sm.BinaryEvent += sm.Substract;            

            Console.WriteLine(sm.Calculate()); // -1, 只保留最後一次調用的傳回值(3,3,-1,-1)
        }                
    }
}

 

(*) 匿名方法
匿名方法的作用是使代碼更簡化,方便了程式員。如果沒有匿名方法,則定義一套事件監聽需要這樣:

class Program
{
    public delegate void SomeDelegate(); // 定義委託
    
    static void Main()
    {
        SomeType obj = new SomeType();
        obj.SomeEvent += new SomeDelegate(MyEventHandler);
    }
    // 一般來說,定義的MyEventHandler方法只用來響應事件,只在上面那一處地方使用
    public static void MyEventHandler() // 定義委託所調用的方法
    {}
}

上面的代碼比較囉嗦,尤其是為委託和它所調用的方法起名字比較費勁我覺得。有了匿名方法以後,僅需要這樣:

class Program
{
    static void Main()
    {
        SomeType obj = new SomeType();
        obj.SomeEvent += delegate{
            // 實現事件處理邏輯
        }; // 注意要有分號
    }    
}

與上一段代碼相比,省去了SomeDelegate和MyEventHandler的定義。

聯繫我們

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