SoFunction
Updated on 2025-03-08

C# basic generic delegation example tutorial

This article describes the usage of generic delegation in C# and analyzes usage in more detail in the form of examples. Share it for your reference. The details are as follows:

First of all, generic delegation is a special form of delegation. Although it looks weird, it is actually similar to delegation when used, but generic delegation is more generic.

Take the most common commission EventHandler in C# as an example. Before .NET 2.0, that is, before generics appeared, ordinary event handling functions were defined by EventHandler, as follows:

public delegate void EventHandler(object sender, EventArgs e);

EventHandler refers to a type of functions that have no return value and have two parameters. The first parameter is object type and the second parameter is EventArgs type.

Due to the introduction of generics in .NET 2.0 and later versions, some built-in classes, interfaces, and delegates have their own generic versions. EventHandler is no exception. It has its own generic version: EventHandler<T>, which is defined as follows:

[Serializable]  
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e) where TEventArgs: EventArgs;

You should find that the type of the second parameter has changed from EventArgs to TEventArgs, and what TEventArgs is is determined by the caller. Assuming that both IntEventArgs and StringEventArgs inherit from, then:

<IntEventArgs> refers to a type of functions: these functions have no return value and have two parameters. The first parameter is object type and the second parameter is IntEventArgs type

<StringEventArgs> refers to a type of functions: these functions have no return value and have two parameters. The first parameter is object type and the second parameter is StringEventArgs type

In fact, EventHandler<IntEventArgs> and EventHandler<StringEventArgs> are two completely different delegates, and the functions they refer to have different signature forms respectively. See the following example:

class IntEventArgs :   
{  
  public int IntValue { get; set; }  
  public IntEventArgs() { }  
  public IntEventArgs(int value)  
  {  = value; }  
}  
 
class StringEventArgs :   
{  
  public string StringValue { get; set; }  
  public StringEventArgs() { }  
  public StringEventArgs(string value)  
  {  = value; }  
}  
 
class Program  
{  
  static void PrintInt(object sender, IntEventArgs e)  
  {  
    ();  
  }  
 
  static void PrintString(object sender, StringEventArgs e)  
  {  
    ();  
  }  
 
  static void Main(string[] args)  
  {  
    EventHandler<IntEventArgs> ihandler = new EventHandler<IntEventArgs>(PrintInt);  
    EventHandler<StringEventArgs> shandler = new EventHandler<StringEventArgs>(PrintString);  
 
    ihandler(null, new IntEventArgs(100));  
    shandler(null, new StringEventArgs("Hello World"));  
  }  
}  

Regarding the specific characteristics of generics and their application in object-oriented thinking, this website has detailed interpretation of relevant articles. Interested readers can check and refer to them.