6.5. Delegates in the Framework

Given the nature of generic delegates, you can imagine that it is now easier to predefine a series of general-purpose delegates that can service the needs of a wide variety of situations. In fact, as part of introducing generics into the .NET Framework, Microsoft has added a series of generic delegate types into the System namespace that are leveraged by the BCL (covered in Chapter 8, "BCL Generics"). These same delegate types are also likely to be of use to you in your own solutions. The goal of this section is to briefly introduce each of these delegate types so you might have a better awareness of what's available out-of-the-box.

NOTE

This section uses the C# notation (delegate<T>) to identify each delegate type. Although these delegates are supported by other .NET languages, the documentation seems to most frequently be standardized around the C# notation.

6.5.1. Action<T>

The Action<T> delegate is generally used in situations where you want to perform some action on an object. As an example, the List<T> class directly leverages this delegate as part of its ForEach() method, allowing you to invoke a delegate on all the items in the list with a single call. This delegate, in fact, would likely be used in place of the delegate that was created in the previous example.

6.5.2. Comparison<T>

No delegate list would be complete without a generic type that supports the comparison of two objects. The framework fills this void via the Comparison<T> ...

Get Professional .NET 2.0 Generics 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.