Re: [問題] 使用Lambda註冊的Event最後怎麼了?

作者: VVll (李奧納多皮卡丘)   2014-04-01 04:30:18
※ 引述《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();
}
}

Links booklink

Contact Us: admin [ a t ] ucptt.com