Delegates:
----------
Delegates are references to methods.
Syntax:
visibility-mode delegate-return-type delegate-name(parameters-list);
Example:
public delegate int mydel(int a,int b);
Here delegates declared is converted internally into a class and that class will be made as a sub class of System.MulticastDelegate and Sysytem.MulticastDelegate is a sub class of System.Delegate;
Every delegate declaration is associated with a method or prototype and it contains a method Invoke().
Invoke() signature will be same as that of the delegate.
class arith_oper
{
public static int add(int a, int b)
{
return(a + b);
}
public static int prod(int a, int b)
{
return(a * b);
}
}
class Program
{
public delegate int mydel(int a,int b);
static void Main(string[] args)
{
mydel m1 = new mydel(arith_oper.add);
mydel m2 = new mydel(arith_oper.prod);
Console.WriteLine("Sum: " + m1(2 - 3));
Console.WriteLine("Product: " + m2.Invoke(6 - 8));
}
}
No comments:
Post a Comment