11.8. Understanding Generic Delegates

Recall from the previous chapter that C# does allow you to define generic delegate types. For example, assume you wish to define a delegate type that can call any method returning void and receiving a single parameter. If the argument in question may differ, you could model this using a type parameter. To illustrate, consider the following code within a new Console Application named GenericDelegate:

namespace GenericDelegate
{
  // This generic delegate can call any method
  // returning void and taking a single type parameter.
  public delegate void MyGenericDelegate<T>(T arg);
class Program { static void Main(string[] args) { Console.WriteLine("***** Generic Delegates *****\n"); // Register targets. MyGenericDelegate<string> ...

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.