anonymous method is a method without a name. Anonymous methods in C# can be defined using the delegate keyword and can be assigned to a variable of delegate type.
Example:
public delegate int DelegateResultNum(int p);
static int num = 100;
delegate int Getnummber(int x, int Y);
public static int Add(int x)
{
num += x;
return num;
}
public static int multiple(int y)
{
num *= y;
return num;
}
public static int getnum()
{
return num;
}
static void Main(string[] args)
{
DelegateResultNum num1 = new DelegateResultNum(Add);
//Anonymous Method
Getnummber d1 = delegate (int r, int z) { return r * z; };
d1(4,5);
num1(12); ;
int x = getnum();
Console.WriteLine(x);
}
No comments:
Post a Comment