বেনামী পদ্ধতি হল একটি নাম ছাড়া পদ্ধতি. এই পদ্ধতিগুলি একটি প্রতিনিধি প্যারামিটার হিসাবে একটি কোড ব্লক পাস করার একটি কৌশল প্রদান করে৷
ডেলিগেট ইনস্ট্যান্স তৈরি করার সাথে সাথে ডেলিগেট কীওয়ার্ড দিয়ে বেনামী পদ্ধতি ঘোষণা করা হয়।
উদাহরণ
using System;
delegate void Demo(int n);
namespace DelegateAppl {
class TestDelegate {
static int num = 50;
public static void AddNum(int p) {
num += p;
Console.WriteLine("Named Method: {0}", num);
}
public static void MultNum(int q) {
num *= q;
Console.WriteLine("Named Method: {0}", num);
}
public static int getNum() {
return num;
}
static void Main(string[] args) {
//create delegate instances using anonymous method
Demo d = delegate(int x) {
Console.WriteLine("Anonymous Method: {0}", x);
};
//calling the delegate using the anonymous method
d(100);
//instantiating the delegate using the named methods
d = new Demo(AddNum);
//calling the delegate using the named methods
d(5);
//instantiating the delegate using another named methods
d = new Demo(MultNum);
//calling the delegate using the named methods
d(2);
Console.ReadKey();
}
}
} আউটপুট
Anonymous Method: 100 Named Method: 55 Named Method: 110
নিচে আমাদের বেনামী পদ্ধতি।
Demo d = delegate(int x) {
Console.WriteLine("Anonymous Method: {0}", x);
};