读书人

事件嘱托-

发布时间: 2013-09-26 10:32:35 作者: rapoo

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


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

读书人网 >C#

热点推荐