日期:2014-05-17  浏览次数:20776 次

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

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