Wednesday, 7 November 2012

Program to illustrate how a delegate is created and used in a program (Single-cast 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 AddOp = new ArithOp(MathOperation.Add);
  ArithOp SubOp = new ArithOp(MathOperation.Sub);
  int result1 = AddOp(200,100);
  int result2 = SubOp(200,100);
  Console.WriteLine("Result 1 = "+result1+"\nResult 2 = "+result2);
  Console.ReadKey();
 }
}

Output:-

No comments:

Post a Comment