打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
delegate vs. event (多谢Cavingdeep兄的指正,等待更新……)
{
    public delegate void TestDelegate(string s);

    public interface ITest {
        // 【区别】 1. event可以用在interface中,而plain delegate不可以(因为它是field)
        event TestDelegate TestE;   // Passed
        TestDelegate TestD;         // Error: Interfaces cannot contain fields
    }

    public class Parent {
        public event TestDelegate TestE;
        public TestDelegate TestD;

        protected void RaiseTestE(string s)
        {
            TestE(s);   // The event 'EventAndDelegate.Parent.TestE' can only
                        
// be used from within the type 'EventAndDelegate.Parent'
        }
    }

    public class Child : Parent {
        void ChildFunc()
        {
            // 【区别】 2. event不允许在声明它的class之外(即使是子类)被调用(除此之外只能用于+=或-=),而plain delegate则允许
            TestD("OK");        // Passed
            TestE("Failure");   // Error: The event 'EventAndDelegate.Parent.TestE' can only appear on
                                
//        the left hand side of += or -= (except when used from within
                                
//        the type 'EventAndDelegate.Parent')

            
// 【补充】 在子类中要触发父类声明的event,通常的做法是在父类中声明一个protected的Raisexxx方法供子类调用
            RaiseTestE("OK");   // The class 'EventAndDelegate.Child' can only call the
                                
// 'EventAndDelegate.ParentRaiseTestE' method to raise the event
                                
// 'EventAndDelegate.Parent.TestE'

            
// 【区别】 同2#
            object o1 = TestD.Target;
            object o2 = TestE.Target;   // The class 'EventAndDelegate.Child' can only call the
                                        
// 'EventAndDelegate.ParentRaiseTestE' method to raise the event
                                        
// 'EventAndDelegate.Parent.TestE'

            
// 【区别】 同2#
            TestD.DynamicInvoke("OK");
            TestE.DynamicInvoke("OK");   // The class 'EventAndDelegate.Child' can only call the
                                        
// 'EventAndDelegate.ParentRaiseTestE' method to raise the event
                                        
// 'EventAndDelegate.Parent.TestE'
        }
    }

    class Other
    {
        static void Main(string[] args)
        {
            Parent p = new Parent();

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

            
// 【区别】 3. event不允许使用赋值运算符,而plain delegate则允许。
            
//             注意,对plain delegate,使用赋值运算符意味着进行了一次替换操作!
            p.TestD = new TestDelegate(p_Test2);   // Passed
            p.TestE = new TestDelegate(p_Test2);   // Error: The event 'EventAndDelegate.Parent.TestE' can only appear on
                                                   
//        the left hand side of += or -= (except when used from within
                                                   
//        the type 'EventAndDelegate.Parent')

            
// 【区别】 同2#
            p.TestD("OK");      // Passed
            p.TestE("Failure"); // Error: The event 'EventAndDelegate.Parent.TestE' can only appear on
                                
//        the left hand side of += or -= (except when used from within
                                
//        the type 'EventAndDelegate.Parent')
        }

        static void p_Test1(string s)
        {
            Console.WriteLine("p_Test1: " + s);
        }

        static void p_Test2(string s)
        {
            Console.WriteLine("p_Test2: " + s);
        }
    }
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
我对.NET中delegate和event区别的理解
【   】【转帖】谈C#中的Delegate
C#基础知识学习之 ✨ 委托(delegate) 与 事件(event) 之间的介绍
C#语法:委托,架构的血液
浅谈.NET中的委托
Qt文件拖放操作
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服