c# 事件的订阅发布Demo
程序员文章站
2023-12-25 21:47:03
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); } }