Delegate and Events
从开始接触EVENT,到DELEGATE已经有若干年了。
但是真正在项目中应用DELEGATE,和EVENT ,缺寥寥无几。 近日,在此在一些项目中看到。
不免又勾起对DELEGATE和EVENT的应用产生兴趣。
再而习之。得出以下结论。
1, DELEGATE的定义只是对Method的模式一个定义。而Method的模式仅仅是指其,para,return value .
(故此,一个DELEGATE的定义可以对多个Method,前提是不同Method拥有相同的Para,和相同的return Type)
2. EVENT其名为事件,也可意味通知,中间作用。应用的时候,可以理解成EVENT继承了DELEGATE,因此在初次
初始化时, EVENT1 += NEW DELEGATE1(Method1)。 定义的时候,将DELEGATE理解为泛型,而EVENT只是
简单的如下定义 PUBLIC DELEGATE1 EVENT1 ;
3. 单独定义DELEGATE,尽管可以单独应用,但不能充分发挥其1对多的特性。而此时,在一些特许领域,通过EVENT
则可以简单的实现一对多的功能。 正如:2中 "EVENT1 += "的描述。
4. 在没有必要的情况下DELEGATE与EVENT本来没有实际的存在意义。
5. 因为DELEGATE本身就是一个泛型,因此再很多情况下,有人使用它的特征来实现匿名函数的功能
如:
delegate void TestDelegate(string s);TestDelegate testDelB = delegate(string s) { Console.WriteLine(s); };
TestDelegate testDelC = (x) => { Console.WriteLine(x); };
当然,后期C#3.0后Lambda 式被广泛应用后,用Lambda 来代替这种匿名已成为普遍,而DELEGATE等的应用空间
几乎被进一步压缩。
后面附一例子:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace Example_17_2_ _ _ _Delegates_and_Events { // a class to hold the information about the event // in this case it will hold only information // available in the clock class, but could hold // additional state information public class TimeInfoEventArgs : EventArgs { public int hour; public int minute; public int second; public TimeInfoEventArgs(int hour, int minute, int second) { this.hour = hour; this.minute = minute; this.second = second; } } // The publisher: the class that other classes // will observe. This class publishes one delegate: // SecondChangeHandler. public class Clock { private int hour; private int minute; private int second; // the delegate the subscribers must implement public delegate void SecondChangeHandler(object clock, TimeInfoEventArgs timeInformation); // an instance of the delegate public SecondChangeHandler SecondChanged; // set the clock running // it will raise an event for each new second public void Run( ) { for (; ; ) { // sleep 100 milliseconds Thread.Sleep(100); // get the current time System.DateTime dt = System.DateTime.Now; // if the second has changed // notify the subscribers if (dt.Second != second) { // create the TimeInfoEventArgs object // to pass to the subscriber TimeInfoEventArgs timeInformation = new TimeInfoEventArgs(dt.Hour, dt.Minute, dt.Second); // if anyone has subscribed, notify them if (SecondChanged != null) { SecondChanged(this, timeInformation); } } // update the state this.second = dt.Second; this.minute = dt.Minute; this.hour = dt.Hour; } } } // A subscriber: DisplayClock subscribes to the // clock's events. The job of DisplayClock is // to display the current time public class DisplayClock { // given a clock, subscribe to // its SecondChangeHandler event public void Subscribe(Clock theClock) { theClock.SecondChanged += new Clock.SecondChangeHandler(TimeHasChanged); } // the method that implements the // delegated functionality public void TimeHasChanged(object theClock, TimeInfoEventArgs ti) { Console.WriteLine("Current Time: {0}:{1}:{2}", ti.hour.ToString( ), ti.minute.ToString( ), ti.second.ToString( )); } } // a second subscriber whose job is to write to a file public class LogCurrentTime { public void Subscribe(Clock theClock) { theClock.SecondChanged += new Clock.SecondChangeHandler(WriteLogEntry); } // this method should write to a file // we write to the console to see the effect // this object keeps no state public void WriteLogEntry(object theClock, TimeInfoEventArgs ti) { Console.WriteLine("Logging to file: {0}:{1}:{2}", ti.hour.ToString( ), ti.minute.ToString( ), ti.second.ToString( )); } } public class Tester { public void Run( ) { // create a new clock Clock theClock = new Clock( ); // create the display and tell it to // subscribe to the clock just created DisplayClock dc = new DisplayClock( ); dc.Subscribe(theClock); // create a Log object and tell it // to subscribe to the clock LogCurrentTime lct = new LogCurrentTime( ); lct.Subscribe(theClock); // Get the clock started theClock.Run( ); } } public class Program { public static void Main( ) { Tester t = new Tester( ); t.Run( ); } } }