Esempio n. 1
0
        private static void Task9()
        {
            Console.WriteLine("\tThe use of events");
            var dispacher = new Dispacher();

            dispacher.eventDouble += (a, b) =>
            {
                if (b != 0)
                {
                    return(a / b);
                }
                throw new DivideByZeroException();
            };
            double n1 = 5.7, n2 = 3.2;

            Console.WriteLine($"{n1} / {n2} = { dispacher.OnEventDouble(n1, n2)}");
            Console.WriteLine(" Using a local variable");
            int number = 5;

            dispacher.eventVoid += n => Console.WriteLine($"{number} + {n} = { number + n}");
            dispacher.OnEventVoid();
            dispacher.OnEventVoid(6);
            Console.WriteLine("\tThe use of a delegate");
            AnonimDelegateVoid voidDel = () => Console.WriteLine("Ok!");

            voidDel += () => Console.WriteLine("Bye!");
            voidDel();
        }
Esempio n. 2
0
        private static void Task8()
        {
            Console.WriteLine("\tThe use of events");
            Dispacher dispacher = new Dispacher();

            // анонимный метод
            dispacher.eventDouble +=
                delegate(double a, double b)
            {
                if (b != 0)
                {
                    return(a / b);
                }
                throw new DivideByZeroException();
            };
            double n1 = 5.7, n2 = 3.2;

            Console.WriteLine($"{n1} / {n2} = { dispacher.OnEventDouble(n1, n2)}"); // вызов
            Console.WriteLine(" Using a local variable");
            int number = 5;

            dispacher.eventVoid += delegate(int n) // анонимный
                                                   // метод
            {
                Console.WriteLine($"{number} + {n} = { number + n}");
            };
            dispacher.OnEventVoid();
            dispacher.OnEventVoid(6);
            Console.WriteLine("\tThe use of a delegate");
            AnonimDelegateVoid voidDel = new AnonimDelegateVoid(delegate
            {
                Console.WriteLine("Ok!");
            });

            voidDel += delegate
            {
                Console.WriteLine("Bye!");
            };
            voidDel();
        }