Exemple #1
0
        static void Main(string[] args)
        {
            // Console.WriteLine("Hello World!");
            PerformCalculation pc = Multiply;

            System.Console.WriteLine(pc(5.0, 6.0));

            pc += divide;
            pc += Subtract;
            // System.Console.WriteLine(pc(5.0, 6.0));

            foreach (var item in pc.GetInvocationList())
            {
                System.Console.WriteLine(item.DynamicInvoke(5.0, 6.0));
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            PerformCalculation pc = Multiply; //[0] of invocationList

            pc  = pc + Subtract;              // [1] of invocationList
            pc += Divide;                     // [2] of invocationList

            //pc -= Subtract;//take one method out of the list.
            pc -= Multiply;

            NewMethod nm = new NewMethod();

            pc += nm.NewClassMethod;

            SampleObject sampleO = new SampleObject();// the object that will be sent in to the delegate

            sampleO.X     = 10.0;
            sampleO.Y     = 100.0;
            sampleO.Total = 0;

            pc(sampleO);                                                                               // fire off the delegate

            System.Console.WriteLine($"The 'Total' Property in the SampleObject is {sampleO.Total}."); //gives the result of Divide() only

            System.Console.WriteLine("\nStarting ForEach loop.\n");
            //double result1 = 0;
            foreach (Delegate item in pc.GetInvocationList())
            {
                if (sampleO.Total == 0)
                {
                    item.DynamicInvoke(sampleO);
                    //System.Console.WriteLine(result1);
                }
                else
                {
                    item.DynamicInvoke(sampleO);
                    // System.Console.WriteLine(result1);
                }
            }

            System.Console.WriteLine($"\tsample.Total is {sampleO.Total}.");
        }