A simple generic delegate : Generic delegate « Generics « C# / C Sharp






A simple generic delegate

A simple generic delegate

using System;

// Declare a generic delegate.
delegate T GenericDelegate<T>(T v);

class GenDelegateDemo {
  static int sum(int v) {
    int result = 0;
    for(int i=v; i>0; i--)
      result += i;

    return result;
  }

  static string reflect(string str) {
    string result = "";

    foreach(char ch in str)
      result = ch + result;

    return result;
  }

  public static void Main() {
    GenericDelegate<int> intDel = sum;
    Console.WriteLine(intDel(3));

    GenericDelegate<string> strDel = reflect;
    Console.WriteLine(strDel("Hello"));
  }
}
           
       








Related examples in the same category

1.Convert event to use generic delegateConvert event to use generic delegate
2.Generic Delegate and non-generic delegate
3.Generics and Delegates
4.a generic delegate being initialized with a normal method
5.Generic Anonymous Methods