event 自定义事件

自定义事件

 1  public class Program
 2     {
 3        public event EventHandler ehdl=null;
 4         public Program() 
 5         {
 6             ehdl += Program_ehdl;
 7         }
 8 
 9         void Program_ehdl(object sender, EventArgs e)
10         {
11             var test=e as TestEventArgs;
12             Console.WriteLine("姓名:{0},年龄:{1},性别:{2}",test.Name,test.Age,test.Gender);
13         }
14 
15        
16         static void Main(string[] args)
17         {
18             Program p = new Program();
19             p.Fly();
20             
21             Console.ReadKey();
22         }
23 
24         private  void Fly()
25         {
26             TestEventArgs test = new TestEventArgs();
27             test.Name = "张三";
28             test.Age = "15";
29             test.Gender="";
30             ehdl(this, test);
31            
32         }
33       
34     }
35     public class TestEventArgs : EventArgs 
36     {
37         public string Name { get; set; }
38         public string Age { get; set; }
39         public string Gender { get; set; }
40     }
View Code

EventHandler  委托,自己也可以定义一个;

 EventArgs

原文地址:https://www.cnblogs.com/zlp520/p/3837326.html