Anonymous MethodsandDelegates
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 anonymousmethods 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 anonymousmethods 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 AnonymousMethods
Anonymous methods have several idiosyncracies that you should be aware of. They
include the following:
• Any parameters needed are specified in braces following the delegate keyword.
For example:
• control.Add(delegate(int param1, string param2) { /* code that uses param1 and
param2 */
});
• Anonymousmethods can return values, but the return type must match that of the
delegate they are being added to.
• You can use the –= operator to remove a method from a delegate variable.
However, it does not make much sense to do so as it will not remove anything.
Anonymous methods that happen to contain the same code are actually different
instances of the code.
• The code in an anonymous method is ordinary C# code. It can comprise multiple
statements, method calls, variable definitions, and so on.
• Variables defined in an anonymous method go out of scope when the method
finishes.
• An anonymous method can access and modify variables in scope when the
anonymous method is defined. Variables manipulated in this way are known as
captured outer variables. Be very careful with this feature!
.
Anonymous Methods and Delegates
All the examples of adding a method to a delegate that. different from the FinishWelding and PaintOff methods, therefore we cannot
use the same delegate to handle all three methods.
Creating a Method Adapter