Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("***A case study with overloaded methods.***");
            DelegateWithTwoIntParameterReturnInt delOb = Sum;

            Console.WriteLine("\nCalling Sum(..) method using a delegate.");
            int total = delOb(10, 20);

            Console.WriteLine("Sum of 10 and 20 is: {0}", total);
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("***A simple delegate demo.***");
            Console.WriteLine("\n Calling Sum(..) method without using a delegate:");
            Console.WriteLine("Sum of 10 and 20 is : {0}", Sum(10, 20));

            //Creating a delegate instance
            //DelegateWithTwoIntParameterReturnInt delOb = new DelegateWithTwoIntParameterReturnInt(Sum);
            //Or,simply write as follows:
            DelegateWithTwoIntParameterReturnInt delOb = Sum;

            Console.WriteLine("\nCalling Sum(..) method using a delegate.");
            int total = delOb(10, 20);

            Console.WriteLine("Sum of 10 and 20 is: {0}", total);

            //Alternative way to calculate the aggregate of the numbers.
            //delOb(25,75) is shorthand for delOb.Invoke(25,75)
            Console.WriteLine("\nUsing Invoke() method on delegate instance, calculating sum of 25 and 75.");
            total = delOb.Invoke(25, 75);
            Console.WriteLine("Sum of 25 and 75 is: {0}", total);
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("***Comparing the behavior of a static method and  instance method when assign them to a delegate instance.***");

            Console.WriteLine("Assigning a static method to a delegate object.");
            //Assigning a static method to a delegate object.
            DelegateWithTwoIntParameterReturnInt delOb = Sum;

            Console.WriteLine("Calling Sum(..) method of Program Class using a delegate.");
            int total = delOb(10, 20);

            Console.WriteLine("Sum of 10 and 20 is: {0}", total);
            Console.WriteLine("delOb.Target={0}", delOb.Target);
            Console.WriteLine("delOb.Target==null? {0}", delOb.Target == null); //true
            Console.WriteLine("delOb.Method={0}", delOb.Method);                //delOb.Method=Int32 Sum(Int32, Int32)

            OutSideProgram outsideOb = new OutSideProgram();

            ////For Q&A 1.9
            //ConsGenerator consGenerator = () =>
            //{
            //    return new OutSideProgram();
            //};
            //consGenerator();

            Console.WriteLine("\nAssigning an instance method to a delegate object.");
            //Assigning an instance method to a delegate object.
            delOb = outsideOb.CalculateSum;
            Console.WriteLine("Calling CalculateSum(..) method of OutsideProgram class using a delegate.");
            total = delOb(50, 70);
            Console.WriteLine("Sum of 50 and 70 is: {0}", total);
            Console.WriteLine("delOb.Target={0}", delOb.Target);                //delOb.Target=DelegateEx1.OutSideProgramClass
            Console.WriteLine("delOb.Target==null? {0}", delOb.Target == null); //false
            Console.WriteLine("delOb.Method={0}", delOb.Method);                //delOb.Method=Int32 Sum(Int32, Int32)
            Console.ReadKey();
        }