event 自定義事件

自定義事件ide

 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  委託,本身也能夠定義一個;this

 EventArgsspa

相關文章
相關標籤/搜索