Unsubscribe anonymous method in C#

Is it possible to unsubscribe an anonymous method from an event? If I subscribe to an event like this: void MyMethod() { Console.WriteLine(“I did it!”); } MyEvent += MyMethod; I can un-subscribe like this: MyEvent -= MyMethod; But if I subscribe using an anonymous method: MyEvent += delegate(){Console.WriteLine(“I did it!”);}; is it possible to unsubscribe … Read more

How can I make a weak protocol reference in ‘pure’ Swift (without @objc)

weak references don’t seem to work in Swift unless a protocol is declared as @objc, which I don’t want in a pure Swift app. This code gives a compile error (weak cannot be applied to non-class type MyClassDelegate): class MyClass { weak var delegate: MyClassDelegate? } protocol MyClassDelegate { } I need to prefix the … Read more

Why would you use Expression rather than Func?

I understand lambdas and the Func and Action delegates. But expressions stump me. In what circumstances would you use an Expression<Func<T>> rather than a plain old Func<T>? 1Best Answer 11 When you want to treat lambda expressions as expression trees and look inside them instead of executing them. For example, LINQ to SQL gets the … Read more