首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c#委托,初学者有关问题

2011-12-28 
c#委托,菜鸟问题namespace TestEvent{class MyDeleChain{// 定义委托public delegate void MeDelegate()/

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();
  }
  } 


  }
}


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

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

热点排行