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