日期:2014-05-18  浏览次数:20791 次

c#委托,菜鸟问题
namespace TestEvent
{
  class MyDeleChain
  {
  // 定义委托
  public delegate void MeDelegate();
  // 定义事件
  public event MeDelegate NotifyEveryOne;
   
  public void Notify()
  {
  // 如果事件不为 null
  while (NotifyEveryOne != null) <-------------把if 改为了while
  {
  Console.WriteLine("触发事件:");
  // 触发事件
  NotifyEveryOne();
  }
  } 


  }
}


那么输出会无限的死循环,为什么?

------解决方案--------------------
委托链可以理解为事件上注册了多个方法 当执行事件时候 可以保证执行这些方法 但是执行的顺序不能保证.所以几个方法中不能有依赖其他方法的语句.