委托和事件温习
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestEvent { class MyClass { public delegate void CompletedEventHandler(); public event CompletedEventHandler WorkCompleted; public void Fire() { if (this.WorkCompleted != null) { this.WorkCompleted(); } } } class Program { static void TestEvent() { Console.WriteLine("test event"); } static void TestEvent2() { Console.WriteLine("test event2"); } static void TestEvent3() { Console.WriteLine("test event3"); } static void TestDelegate() { Console.WriteLine("test delegate"); } static void Main(string[] args) { MyClass myObject = new MyClass(); myObject.WorkCompleted += TestEvent; myObject.WorkCompleted += TestEvent2; myObject.WorkCompleted += TestEvent3; myObject.WorkCompleted -= TestEvent3; myObject.Fire(); Console.ReadLine(); } } }