Previous Page
Next Page

Anonymous Methods and Delegates

All the examples of adding a method to a delegate that you have seen so far use the method's name. For example, returning to the automated factory scenario shown earlier, to add the StopFolding method of the folder object to the stopMachinery delegate, we did this:

this.stopMachinery += folder.StopFolding;

This approach is very useful if there is a convenient method that matches the signature of the delegate, but what if this is not the case? Suppose that the StopFolding method actually had the following signature:

void StopFolding(int shutDownTime); // Shut down within the specified number of seconds

This is now different from the FinishWelding and PaintOff methods, therefore we cannot use the same delegate to handle all three methods.

Creating a Method Adapter

The way around this problem is to create another method that calls StopFolding, but that takes no parameters itself, like this:

void FinishFolding()
{
    folder.StopFolding(0); // Shutdown immediately
}
NOTE
The FinishFolding method is a classic example of an Adapter; a method that converts (or adapts) a method to give it a different signature. This pattern is very common, and is one of the set of patterns documented in the book Design Patterns: Elements of Reusable Object-Oriented Architecture by Gamma, Helm, Johnson, and Vlissides (Addison-Wesley Professional; 1994).

In many cases, adapter methods such as this are small, and it is easy to lose them in a sea of methods, especially in a large class. Furthermore, apart from using it to adapt the StopFolding method for use by the delegate, it is unlikely to be called elsewhere. C# provides anonymous methods for situations such as this.

Using an Anonymous Method as an Adapter

An anonymous method is a method that does not have a name. This sounds very strange, but anonymous methods are actually quite useful.

There might well be occassions when you have a block of code that you are never going to call directly, but you would like to be able to invoke using a delegate. The FinishFolding method discussed just now is an example; its sole purpose is to provide an adapter for the StopFolding method. Thinking up a name for the method is really just an overhead for the developer (albeit a small one), but consider what happens if there are a number of these adapter methods in an application—they start to clutter up the classes. You can use an anonymous method anywhere that you can use a delegate. You simply provide the code, enclosed in curly braces, and prefixed with the delegate keyword. To use an anonymous method as an adapter for the StopFolding method and add it to the stopMachinery delegate, you can write this:

this.stopMachinery += delegate { folder.StopFolding(0); };

You no longer need to create the FinishFolding method.

You can also pass an anonymous method as a parameter in place of a delegate, like this:

control.Add(delegate { folder.StopFolding(0); } );
Features of Anonymous Methods

Anonymous methods have several idiosyncracies that you should be aware of. They include the following:


Previous Page
Next Page