বেনামী পদ্ধতি একটি প্রতিনিধি প্যারামিটার হিসাবে একটি কোড ব্লক পাস করার একটি কৌশল প্রদান করে। বেনামী পদ্ধতি হল একটি নাম ছাড়া পদ্ধতি, শুধুমাত্র শরীর।
আসুন দেখি কিভাবে C# −
-এ বেনামী পদ্ধতি ঘোষণা করা যায়delegate void NumberChanger(int n);
...
NumberChanger nc = delegate(int x) {
Console.WriteLine("Anonymous Method: {0}", x);
}; উদাহরণ
নিম্নলিখিত উদাহরণ C# এ বেনামী পদ্ধতি প্রয়োগ করার জন্য।
using System;
delegate void NumberChanger(int n);
namespace DelegateAppl {
class Demo {
static int num = 10;
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
NumberChanger nc = delegate(int x) {
Console.WriteLine("Anonymous Method: {0}", x);
};
//calling the delegate using the anonymous method
nc(10);
//instantiating the delegate using the named methods
nc = new NumberChanger(AddNum);
//calling the delegate using the named methods
nc(5);
//instantiating the delegate using another named methods
nc = new NumberChanger(MultNum);
//calling the delegate using the named methods
nc(2);
Console.ReadKey();
}
}
} আউটপুট
Anonymous Method: 10 Named Method: 15 Named Method: 30