O'Reilly logo

Pro C# 2010 and the .NET 4 Platform, Fifth Edition by Andrew Troelsen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required