delegate void del(); class MyClass1 { public event del eventcount;//創建事件併發布 public void Count() { for (int i = 0; i < 100; i++) { ... ...
delegate void del(); class MyClass1 { public event del eventcount;//創建事件併發布 public void Count() { for (int i = 0; i < 100; i++) { if (i % 12 == 0 && eventcount != null) { eventcount(); } } } } class MyClass2 { public int sum { get; set; } public MyClass2(MyClass1 myClass1) { sum = 0; myClass1.eventcount += sumj;//訂閱事件 } void sumj() { sum++; } } class Program { static void Main(string[] args) { MyClass1 myClass1 = new MyClass1(); MyClass2 myClass2 = new MyClass2(myClass1); myClass1.Count(); Console.WriteLine(myClass2.sum); } }