首先监视定义类
class Event { public delegate void tempChange(object sender, EventArgs e); public event tempChange OntempChange; string temp; public string Temp { get { return temp; } set { if (temp != value) { OntempChange(this, new EventArgs()); } temp = value; } } }
使用事件:
static Event ven; static void Main(string[] args) { ven = new Event(); ven.OntempChange += new Event.tempChange(ven_OntempChange); for (int i = 0; i < 10; i++) { ven.Temp = i +"aaa"; Thread.Sleep(1000); } } static void ven_OntempChange(object sender, EventArgs e) { Console.WriteLine(ven.Temp +" "); }
输出结果: