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

事件嘱托-

2013-09-26 
事件委托-----------------------public delegate void MyDel()public class myDel{public static event

事件委托-----------------------


    public delegate void MyDel();
    public class myDel
    {
        public static event MyDel catshout;
        public static void ratrun()
        {
            Console.WriteLine("run");
        }
        public static void test()
        {
            catshout += delegate {
                Console.WriteLine(1);
            };
        }
    }
//调用该方法 没有输出"run"
  myDel.test();

为什么呢
[解决办法]
我就没看出来为什么要输出"run",甚至都没发现ratrun()这个方法有被调用的地方。
楼主你知道自己在说什么吗?
[解决办法]
catshout += delegate...至少订阅事件。事件本身还没有得到触发。

[解决办法]
catshout += delegate...只是订阅事件
[解决办法]
只是将事件挂上了,并没有执行 catshout。需要加上。
public static void test()
        {
            catshout += delegate {
                Console.WriteLine(1);
            };
            catshout();
        }

热点排行