Skip to content

Delegate programming

Shraddha Bhat

Asked Apr 25, 2013, 12:51 AM EDT1 answer
Share
LinkedInX
Write a Complete C# program to add and subtract two complex numbers by writing Add and Subtract
methods which could be called through the delegate programming.

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Ashutosh Chaturvedi
    0
    C#
    namespace MyFirstDelegate
    {
        //This delegate can point to any method,
        //taking two integers and returning an
        //integer.
        public delegate int MyDelegate(int x, int y);
        //This class contains methods that MyDelegate will point to.
        public class MyClass
        {
            public static int Add(int x, int y)
            {
                return x + y;
            }
            public static int Multiply(int x, int y)
            {
                return x * y;
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                //Create an Instance of MyDelegate
                //that points to MyClass.Add().
                MyDelegate del1 = new MyDelegate(MyClass.Add);
                //Invoke Add() method using the delegate.
                int addResult = del1(5, 5);
                Console.WriteLine("5 + 5 = {0}\n", addResult);
                //Create an Instance of MyDelegate
                //that points to MyClass.Multiply().
                MyDelegate del2 = new MyDelegate(MyClass.Multiply);
                //Invoke Multiply() method using the delegate.
                int multiplyResult = del2(5, 5);
                Console.WriteLine("5 X 5 = {0}", multiplyResult);
                Console.ReadLine();
            }
        }
    }
    0