1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace del_event_Test
8 {
9 public delegate void OnFileChange();//定义委托
10 public class FileListen
11 {
12 public event OnFileChange OnFileChangeEvent;//定义事件
13 public void test()
14 {
15 for(int i=1;i<=100;i++)
16 {
17 if(i==55)
18 {
19 //事件被绑定时才能触发事件
20 if (OnFileChangeEvent != null)
21 {
22 OnFileChangeEvent(); //事件发生
23 }
24 }
25 }
26 }
27 }
28
29 class Program
30 {
31 static void Main(string[] args)
32 {
33 FileListen fl = new FileListen();
34 fl.OnFileChangeEvent += OutPut;//绑定具体方法到事件
35 fl.test();
36 }
37
38 private static void OutPut()
39 {
40 Console.WriteLine("这是循环到55时发生的事件!");
41 Console.ReadKey();
42 }
43 }
44 }