コード例 #1
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}.");
        }
コード例 #2
0
 public void NewClassMethod(SampleObject x)
 {
     x.Total = Math.Pow(x.X, x.Y);
 }
コード例 #3
0
 //method 3
 public static void Divide(SampleObject x)
 {
     x.Total = x.X / x.Y;
 }
コード例 #4
0
 //method 2
 public static void Subtract(SampleObject x)
 {
     x.Total = x.X - x.Y;
 }
コード例 #5
0
 // method 1
 public static void Multiply(SampleObject x)
 {
     x.Total = x.X * x.Y;
 }
コード例 #6
0
 public void NewClassMethod(SampleObject x)
 {
     //x.Total = Math.Pow(x.X, x.Y);
     Console.WriteLine($"In the NewClassMethod Method");
 }
コード例 #7
0
 //method 3
 public static void Divide(SampleObject x)
 {
     x.Total = x.X / x.Y;
     Console.WriteLine($"In the Divide Method");
 }
コード例 #8
0
 //method 2
 public static void Subtract(SampleObject x)
 {
     x.Total = x.X - x.Y;
     Console.WriteLine($"In the Subtract Method");
 }
コード例 #9
0
 // method 1
 public static void Multiply(SampleObject x)
 {
     x.Total = x.X * x.Y;
     Console.WriteLine($"In the Multiply Method");
 }