※ 引述《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();
}
}