作者VVll (信)
看板C_Sharp
标题Re: [问题] 使用Lambda注册的Event最後怎麽了?
时间Tue Apr 1 04:30:18 2014
※ 引述《yan04870449 (杨伯)》之铭言:
: class MyClass
: {
: static public event Action OnCall;
: }
: class Car
: {
: public void Start()
: {
: MyClass.OnCall += () =>
: {
: //做一些事情
: };
: }
: }
: 上面只是用来说明这个问题而已
: 范例中,我使用Lambda再Car的instance里面注册了事件
: 但我并没有注销这个事件,当这Car的instance被移除了
: 之後这个被注册进去的匿名函式跑哪去了?
: 然後当OnCall事件又发生的时候,会发生甚麽事吗?
void main()
{
MSG m1 = new MSG();
m1.setAction(() =>
{
Console.WriteLine("hi");
});
m1 = null;
MSG m2 = new MSG();
m2.doAction();
}
public class MSG
{
static Action action;
public void setAction(Action tmp)
{
action = tmp;
}
public void doAction()
{
action();
}
}
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 123.240.239.100
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/C_Sharp/M.1396297821.A.C1B.html