C#中的取消订阅匿名方法是否可以从事件中取消订阅匿名方法?如果我订阅这样的活动:void MyMethod(){
Console.WriteLine("I did it!");}MyEvent += MyMethod;我可以这样取消订阅:MyEvent -= MyMethod;但如果我使用匿名方法订阅:MyEvent += delegate(){Console.WriteLine("I did it!");};是否可以取消订阅此匿名方法?如果是,怎么做?
3 回答
Cats萌萌
TA贡献1805条经验 获得超9个赞
Action myDelegate = delegate(){Console.WriteLine("I did it!");};MyEvent += myDelegate;// .... laterMyEvent -= myDelegate;
一只萌萌小番薯
TA贡献1795条经验 获得超7个赞
MyEventHandler foo = null;foo = delegate(object s, MyEventArgs ev) { Console.WriteLine("I did it!"); MyEvent -= foo; };MyEvent += foo;
倚天杖
TA贡献1828条经验 获得超3个赞
- 3 回答
- 0 关注
- 924 浏览
添加回答
举报
0/150
提交
取消