Friday, 1 September 2017

C# program to implement the use of Delegates

C# program example to use of implement Delegates in C# program

Program

using System;
delegate int calc(int x,int y);
class oper    {
       public static int add(int x, int y)
       {
           return x + y;
       }
       public static int sub(int x, int y)
       {
            return x - y;
       }
   }
    class Program
    {
        static void Main(string[] args)
        {
            calc c1 = new calc(oper.add);
             calc c2 = new calc(oper.sub);

            int a = c1(50, 40);
            int b = c2(30, 10);

            Console.WriteLine("Add: {0}", a);
            Console.WriteLine("Sub: {0}", b);
        }

    } 

Output


0 comments:

Post a Comment