Sunday, 3 December 2017

C# Program to Implement Arithmetic Operations using Delegates


Code:

using System;
delegate int NumberChanger(int n);
namespace example
{
    class Delegate
    {
        static int num = 10;
        public static int AddNum(int a)
        {
            num += a;
            return num;
        }

        public static int MultNum(int b)
        {
            num *= b;
            return num;
        }
        public static int getNum()
        {
            return num;
        }

        static void Main(string[] args)
        {

            NumberChanger n1 = new NumberChanger(AddNum);
            NumberChanger n2 = new NumberChanger(MultNum);
            n1(25);
            Console.WriteLine("Value of Num: {0}", getNum());
            n2(5);
            Console.WriteLine("Value of Num: {0}", getNum());
            Console.ReadKey();
        }
    }
}


Output:

Value of Num: 35
Value of Num: 175


More C# Programs:




















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...