Remember to unregister your anonymous delegate!

I love anonymous delegates - I think they are extremely useful and allow me to solve some problems in very elegant ways.  However, once you really get into them, you start to see the dark side of anonymous delegates and that is unregistration. Here's the basic problem: when binding an instance delegate to an event to handle some activity, the delegate will cache off the instance reference - thereby keeping the reference alive.  So for example, if I had a form which wanted to … [Read more...]

Unknown new VS.NET 2005 feature – Calling Delegate.Invoke directly

So, at the GNET2 last week, I found out that you can now call Delegate.Invoke instead of using the C# functor syntax.  This makes code much more readable IMO -- delegate void MyCallback(string msg);...MyCallback callback;...callback += delegate(string msg) { Console.WriteLine(msg); };...callback.Invoke("Hello");   // instead of callback("Hello"); Very nice. … [Read more...]