CSharp - Generic Delegate Types

Introduction

A delegate type may contain generic type parameters.

For example:

public delegate T ConverterFunction<T> (T arg);

By using generic delegate, we can write a generalized Convert utility method that works on any type:

Demo

using System;
delegate T ConverterFunction<T> (T arg);
class Util/* ww w .  j a va2s.c  o  m*/
{
     public static void Convert<T> (T[] values, ConverterFunction<T> t)
     {
         for (int i = 0; i < values.Length; i++)
           values[i] = t (values[i]);
     }
}

class Test
{
     static void Main()
     {
         int[] values = { 1, 2, 3 };
         Util.Convert (values, Square);      
         foreach (int i in values)
           Console.Write (i + "  ");         
     }
     static int Square (int x) => x * x;
}

Result