在使用delegate时,使用event 与不使用的区别
2015-02-04 21:09
344 查看
转自:http://www.cnblogs.com/shensr/archive/2005/11/24/283653.html
首先说明,event其实也是一种delegate,为了区分,我们称一般的delegate为“plain delegate”。
分析:
plain delegate与event的关系类似于field与Property(实事上前者就是field,或者我们可以把event看成是一种特殊的Property)
正是由于1#,在使用上,plain delegate几乎没有任何限制,而event则有严格的限制(只能用在+=和-=的左边)
结论:
event更面向对象一些。
当我们需要灵活时,直接使用plain delegate;反之,需要严格的控制时,使用event。
由于event不能使用赋值运算符,因此有时我们要求一个事件在任何时刻只能有一个响应方法时,我们使用plain delegate更为方便。
首先说明,event其实也是一种delegate,为了区分,我们称一般的delegate为“plain delegate”。
using System; namespace EventAndDelegate { 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); } } }
分析:
plain delegate与event的关系类似于field与Property(实事上前者就是field,或者我们可以把event看成是一种特殊的Property)
正是由于1#,在使用上,plain delegate几乎没有任何限制,而event则有严格的限制(只能用在+=和-=的左边)
结论:
event更面向对象一些。
当我们需要灵活时,直接使用plain delegate;反之,需要严格的控制时,使用event。
由于event不能使用赋值运算符,因此有时我们要求一个事件在任何时刻只能有一个响应方法时,我们使用plain delegate更为方便。
相关文章推荐
- 我对.NET中delegate和event区别的理解
- 一个C#中使用 event 和 delegate的小例子。
- Delegate,Action,Func,Predicate的使用与区别
- Delegate,Action,Func,Predicate的使用与区别
- C# this.Invoke()与Delegate.Invoke()区别-------使用多线程循环执行(定时执行)数据处理功能
- 使用MasterPage,Event,Delegate在ASP.NET2.0 创建公用工具栏
- C#中Delegate和Event以及它们的区别(转载)
- AutoResetEvent和ManualResetEvent的使用与区别
- Delegate与Event的区别[转]
- C#知识点总结系列:3、C#中Delegate和Event以及它们的区别
- delegate和event的区别
- delegate和event的区别
- delegate和Event区别
- [Bindable]与[Bindable("changeEvent")]/[Bindable(event="changeEvent")]区别和使用说明
- Delegate,Action,Func,Predicate的使用与区别
- delegate使用以及event
- ie与firefox下的event使用说明与详细区别
- javascript attachEvent 和 addEventListener 使用方法 区别
- ie与firefox下的event使用说明与详细区别
- C#中Delegate和Event以及它们的区别