Friday, 23 December 2016

Delegate

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OopsConcept
{
    class Delegate
    {
    }
    public delegate int NumberChanger(int w);
    namespace DelegateAppl
    {
        class TestDelegate
        {
            static int num = 10;
            public static int AddNum(int p)
            {
                num += p;
                return num;
            }

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

            static void MainDelegate(string[] args)
            {
                //create delegate instances
                NumberChanger nc1 = new NumberChanger(AddNum);
                NumberChanger nc2 = new NumberChanger(MultNum);

                nc1(25);
                nc2(35);
                Console.WriteLine("Addition of two number is {0}",getNum());
                Console.ReadLine();
            }
        }
    }
}

No comments:

Post a Comment