Generic delegate không cần khai báo delegate statement. Định nghĩa trong namespace System.
Định nghĩa generic delegate với type parameter:
delegate T myDelegate<T>(T n);
Ví dụ
using System;
using System.Collections.Generic;
delegate T myDelegate<T>(T n);
namespace GenericDelegateAppl {
class TestDelegate {
static int num = 5;
public static int AddNum(int p) { num += p; return num; }
public static int MultNum(int q) { num *= q; return num; }
public static int getNum() { return num; }
static void Main(string[] args) {
myDelegate<int> nc1 = new myDelegate<int>(AddNum);
myDelegate<int> nc2 = new myDelegate<int>(MultNum);
nc1(50); Console.WriteLine("Value of Num: {0}", getNum());
nc2(10); Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
}
}