Example #1
0
        static void Main(string[] args)
        {
            Calculator   calc = new Calculator();
            CalcDelegate del  = calc.Add;

            del += Calculator.Sub;
            del += calc.Mult;
            del += calc.Div;
            del -= calc.Mult;
            foreach (CalcDelegate item in del.GetInvocationList())
            {
                Console.WriteLine($"Результат:{item(5.5,4.7)}");
            }

            Delegate[] delArr = del.GetInvocationList();
            if (delArr[2] is CalcDelegate)
            {
                Console.WriteLine((delArr[2] as CalcDelegate).Invoke(5.5, 4.7));
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            CalcDelegate calculator = Calc.Add;

            Console.WriteLine($"{calculator.Method.Name} : {calculator?.Invoke(2, 4)}");
            calculator += Calc.Mult;
            Console.WriteLine($"{calculator.Method.Name} : {calculator?.Invoke(2, 4)}");
            Console.WriteLine("========================");
            foreach (Delegate deleg in calculator.GetInvocationList())
            {
                Console.WriteLine($"{deleg.Method.Name} : {deleg.DynamicInvoke(5,5)}");
            }
        }
Example #3
0
            static void Main(string[] args)
            {
                Calculator calc = new Calculator();

                CalcDelegate del = new CalcDelegate(calc.Add);

                del += new CalcDelegate(Calculator.Sub);
                del += calc.Mult; // групповое преобразование методов
                del += calc.Div;
                Console.WriteLine("Example 1");
                Console.WriteLine($"Result:{del(6.0, 2.0)} \n\n\n");
                Console.WriteLine("Example 2");
                foreach (CalcDelegate item in del.GetInvocationList())
                {
                    Console.WriteLine($"Result:{item(6.0, 2.0)} \n\n\n");
                }
                Console.WriteLine("Example 2");

                Delegate[] delArr = del.GetInvocationList();
                if (delArr[2] is CalcDelegate)
                {
                    Console.WriteLine((delArr[2] as CalcDelegate).Invoke(2, 2));
                }
            }
Example #4
0
        private static void Task1()
        {
            var          calculator = new Calculator();
            CalcDelegate delAll     = calculator.Add;

            delAll += Calculator.Sub;
            delAll += calculator.Mult;
            delAll += calculator.Div;

            Console.WriteLine($"Result: {delAll(5.7, 3.2)}");
            foreach (CalcDelegate item in delAll.GetInvocationList())
            {
                try
                {
                    Console.WriteLine($"Result: {item(5.7, 3.2)}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }