c# delegate ,event

时间:2021-08-07 09:09:52

首先说明,event其实也是一种delegate,为了区分,我们称一般的delegate为“plain delegate”。

写代码的过程中,经常会在delegate和event之间进行选择,以前也没仔细思考选择的原因,今天终于忍不住花了半天时间仔细琢磨了一下……好了,直接拿代码说话吧:

c# delegate ,event

using System;

c# delegate ,event


c# delegate ,event

namespace EventAndDelegate

c# delegate ,event

c# delegate ,event

c# delegate ,event

{

c# delegate ,event

    public delegate void TestDelegate(string s);

c# delegate ,event


c# delegate ,event

c# delegate ,event

    public interface ITest 

c# delegate ,event

{

c# delegate ,event

        // 【区别】 1. event可以用在interface中,而plain delegate不可以(因为它是field)

c# delegate ,event

        event TestDelegate TestE;   // Passed

c# delegate ,event

        TestDelegate TestD;         // Error: Interfaces cannot contain fields

c# delegate ,event

    }

c# delegate ,event


c# delegate ,event

c# delegate ,event

    public class Parent 

c# delegate ,event

{

c# delegate ,event

        public event TestDelegate TestE;

c# delegate ,event

        public TestDelegate TestD;

c# delegate ,event


c# delegate ,event

        protected void RaiseTestE(string s)

c# delegate ,event

c# delegate ,event

        

c# delegate ,event

{

c# delegate ,event

            TestE(s);   // The event ‘EventAndDelegate.Parent.TestE‘ can only

c# delegate ,event

                        // be used from within the type ‘EventAndDelegate.Parent‘

c# delegate ,event

        }

c# delegate ,event

    }

c# delegate ,event


c# delegate ,event

c# delegate ,event

    public class Child : Parent 

c# delegate ,event

{

c# delegate ,event

        void ChildFunc()

c# delegate ,event

c# delegate ,event

        

c# delegate ,event

{

c# delegate ,event

            // 【区别】 2. event不允许在声明它的class之外(即使是子类)被调用(除此之外只能用于+=或-=),而plain delegate则允许

c# delegate ,event

            TestD("OK");        // Passed

c# delegate ,event

            TestE("Failure");   // Error: The event ‘EventAndDelegate.Parent.TestE‘ can only appear on

c# delegate ,event

                                //        the left hand side of += or -= (except when used from within

c# delegate ,event

                                //        the type ‘EventAndDelegate.Parent‘)

c# delegate ,event


c# delegate ,event

            // 【补充】 在子类中要触发父类声明的event,通常的做法是在父类中声明一个protected的Raisexxx方法供子类调用

c# delegate ,event

            RaiseTestE("OK");   // The class ‘EventAndDelegate.Child‘ can only call the

c# delegate ,event

                                // ‘EventAndDelegate.ParentRaiseTestE‘ method to raise the event

c# delegate ,event

                                // ‘EventAndDelegate.Parent.TestE‘

c# delegate ,event


c# delegate ,event

            // 【区别】 同2#

c# delegate ,event

            object o1 = TestD.Target;

c# delegate ,event

            object o2 = TestE.Target;   // The class ‘EventAndDelegate.Child‘ can only call the

c# delegate ,event

                                        // ‘EventAndDelegate.ParentRaiseTestE‘ method to raise the event

c# delegate ,event

                                        // ‘EventAndDelegate.Parent.TestE‘

c# delegate ,event


c# delegate ,event

            // 【区别】 同2#

c# delegate ,event

            TestD.DynamicInvoke("OK");

c# delegate ,event

            TestE.DynamicInvoke("OK");   // The class ‘EventAndDelegate.Child‘ can only call the

c# delegate ,event

                                        // ‘EventAndDelegate.ParentRaiseTestE‘ method to raise the event

c# delegate ,event

                                        // ‘EventAndDelegate.Parent.TestE‘

c# delegate ,event

        }

c# delegate ,event

    }

c# delegate ,event


c# delegate ,event

    class Other

c# delegate ,event

c# delegate ,event

    

c# delegate ,event

{

c# delegate ,event

        static void Main(string[] args)

c# delegate ,event

c# delegate ,event

        

c# delegate ,event

{

c# delegate ,event

            Parent p = new Parent();

c# delegate ,event


c# delegate ,event

            p.TestD += new TestDelegate(p_Test1);   // Passed

c# delegate ,event

            p.TestE += new TestDelegate(p_Test1);   // Passed

c# delegate ,event


c# delegate ,event

            // 【区别】 3. event不允许使用赋值运算符,而plain delegate则允许。

c# delegate ,event

            //             注意,对plain delegate,使用赋值运算符意味着进行了一次替换操作!

c# delegate ,event

            p.TestD = new TestDelegate(p_Test2);   // Passed

c# delegate ,event

            p.TestE = new TestDelegate(p_Test2);   // Error: The event ‘EventAndDelegate.Parent.TestE‘ can only appear on

c# delegate ,event

                                                   //        the left hand side of += or -= (except when used from within

c# delegate ,event

                                                   //        the type ‘EventAndDelegate.Parent‘)

c# delegate ,event


c# delegate ,event

            // 【区别】 同2#

c# delegate ,event

            p.TestD("OK");      // Passed

c# delegate ,event

            p.TestE("Failure"); // Error: The event ‘EventAndDelegate.Parent.TestE‘ can only appear on

c# delegate ,event

                                //        the left hand side of += or -= (except when used from within

c# delegate ,event

                                //        the type ‘EventAndDelegate.Parent‘)

c# delegate ,event

        }

c# delegate ,event


c# delegate ,event

        static void p_Test1(string s)

c# delegate ,event

c# delegate ,event

        

c# delegate ,event

{

c# delegate ,event

            Console.WriteLine("p_Test1: " + s);

c# delegate ,event

        }

c# delegate ,event


c# delegate ,event

        static void p_Test2(string s)

c# delegate ,event

c# delegate ,event

        

c# delegate ,event

{

c# delegate ,event

            Console.WriteLine("p_Test2: " + s);

c# delegate ,event

        }

c# delegate ,event

    }

c# delegate ,event

}

c# delegate ,event


分析:

plain delegate与event的关系类似于field与Property(实事上前者就是field,或者我们可以把event看成是一种特殊的Property)

正是由于1#,在使用上,plain delegate几乎没有任何限制,而event则有严格的限制(只能用在+=和-=的左边)

结论

event更面向对象一些。

当我们需要灵活时,直接使用plain delegate;反之,需要严格的控制时,,使用event。

由于event不能使用赋值运算符,因此有时我们要求一个事件在任何时刻只能有一个响应方法时,我们使用plain delegate更为方便。