জেনেরিক ডেলিগেট ব্যবহার করলে আপনাকে ডেলিগেট স্টেটমেন্ট ডিফাইন করতে হয় না। এগুলো System নেমস্পেসে ডিফাইনড থাকে।
আপনি টাইপ প্যারামিটারসহ একটি জেনেরিক ডেলিগেট ডিফাইন করতে পারেন। উদাহরণস্বরূপ:
delegate T myDelegate<T>(T n);
উদাহরণ
নিচে C#-এ জেনেরিক ডেলিগেট তৈরি করার উদাহরণ দেওয়া হলো:
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)
{
// create delegate instances
myDelegate<int> nc1 = new myDelegate<int>(AddNum);
myDelegate<int> nc2 = new myDelegate<int>(MultNum);
// calling the methods using the delegate objects
nc1(50);
Console.WriteLine("Value of Num: {0}", getNum());
nc2(10);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
}
}