Wednesday, 7 November 2012

Program to illustrate how an array of delegate object is created and used in a program (Singlecast delegate)


using System;
delegate int ArithOp(int x,int y);
class MathOperation
{
 public static int Add(int a,int b)
 {
  return a+b;
 }
 static public int Sub(int a,int b)
 {
  return a-b;
 }
}
class DelegateTest
{
 static void Main()
 {
  ArithOp[] Op = { new ArithOp(MathOperation.Add),
                   new ArithOp(MathOperation.Sub)
                 };
  for(int i=0;i<2;ProcessMethod(Op[i],200,100),i++);
  Console.ReadKey();
 }
 static void ProcessMethod(ArithOp obj,int a,int b)
 {
  int result = obj(a,b);
  Console.WriteLine("Result = "+result);
 }
}

Output:-

No comments:

Post a Comment