11.10. Understanding C# Anonymous Methods
As you have seen, when a caller wishes to listen to incoming events, it must define a custom method in a class (or structure) that matches the signature of the associated delegate, for example:
class Program { static void Main(string[] args) { SomeType t = new SomeType(); // Assume "SomeDelegate" can point to methods taking no // args and returning void. t.SomeEvent += new SomeDelegate(MyEventHandler); } // Typically only called by the SomeDelegate object. public static void MyEventHandler() { // Do something when event is fired. } }
When you think about it, however, methods such as MyEventHandler() are seldom intended to be called by any part of the program other than the invoking delegate. As far as ...
Get Pro C# 2010 and the .NET 4 Platform, Fifth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.