C Foundation Generic delegate example tutorial

  • 2020-10-07 18:51:52
  • OfStack

This article takes an example of the use of generic delegates in C# and analyzes them in more detail in the form of examples. To share with you for your reference. The details are as follows:

First of all, a generic delegate is a special form of a delegate. Although it looks weird, it looks like a delegate when you use it, but a generic delegate is more generic.

Take EventHandler, the most common delegate in C#. Before.NET 2.0, before generics, ordinary event handlers were defined by EventHandler as follows:


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

EventHandler refers to a class of functions that have no return value and take two arguments, the first of which is of type object and the second of type EventArgs.

NET 2.0 and later, due to the introduction of generics, a number of built-in (ES19en-ES20en) classes, interfaces, and delegates have their own version of generics. EventHandler is no exception and has its own generic version: EventHandler < T > , its definition is as follows:


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

You should see that the type of the second argument changes from EventArgs to TEventArgs, while it is up to the caller to decide what TEventArgs is. Assuming that both IntEventArgs and StringEventArgs inherit from System. EventArgs, then:

1.EventHandler < IntEventArgs > Refers to a class of functions that have no return value and take two arguments, the first of which is of type object and the second of type IntEventArgs

2.EventHandler < StringEventArgs > Refers to functions of type 1: These functions have no return value and take two arguments, the first of type object and the second of type StringEventArgs

In fact, EventHandler < IntEventArgs > And EventHandler < StringEventArgs > Are two completely different delegates, each referring to a function with a different signature form. See the following example:


class IntEventArgs : System.EventArgs  
{  
  public int IntValue { get; set; }  
  public IntEventArgs() { }  
  public IntEventArgs(int value)  
  { this.IntValue = value; }  
}  
 
class StringEventArgs : System.EventArgs  
{  
  public string StringValue { get; set; }  
  public StringEventArgs() { }  
  public StringEventArgs(string value)  
  { this.StringValue = value; }  
}  
 
class Program  
{  
  static void PrintInt(object sender, IntEventArgs e)  
  {  
    Console.WriteLine(e.IntValue);  
  }  
 
  static void PrintString(object sender, StringEventArgs e)  
  {  
    Console.WriteLine(e.StringValue);  
  }  
 
  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"));  
  }  
}  

The specific features of generics and their application in object-oriented thinking are explained in detail in the relevant articles on this site. For interested readers, please refer to reference 1 below.


Related articles: