Beispiel #1
0
        private static void DelegatePresentation()
        {
            var a = 3;
            var b = 2;

            IntOperation operation = new IntOperation(SomeFunction);
            //operation = SomeFunction; //or this
            //operation = Math.Min; // or this

            var ret = operation.Invoke(a, b);

            //var ret2 = operation(a, b); //shorter version
            Console.WriteLine("Sum on {0} and {1} is {2}", a, b, ret);

            operation = Math.Max;
            ret       = operation.Invoke(a, b); //subtraction
            Console.WriteLine("Max on {0} and {1} is {2}", a, b, ret);

            Func <int, int, int> func   = SomeFunction; //new SomeFunction<int, int, int>(operation);
            Action <string>      action = Print;

            _action = action;
            action("Text");

            //subscribing to delegate - works only with void delegates
            action += s => { Console.WriteLine("1"); };
            action += s => { Console.WriteLine("s2"); };
            action("");

            ExecuteDelegate(operation);
        }
Beispiel #2
0
        static void Main()
        {
            var a = 3;
            var b = 2;

            var operation = new IntOperation((x, y) => { return(x - y); });
            //operation = new IntOperation((x, y) => x - y);
            //operation = (x, y) => x - y;

            var ret = operation.Invoke(a, b); //sum

            Console.WriteLine("Sum on {0} and {1} is {2}", a, b, ret);

            operation = (x, y) => { Console.WriteLine("sub"); return(x - y); };
            ret       = operation.Invoke(a, b); //subtraction
            Console.WriteLine("Subtraction on {0} and {1} is {2}", a, b, ret);

            operation = (x, y) => { Console.WriteLine("prod"); return(x * y); }; //what about += events
            ret       = operation.Invoke(a, b);                                  //product
            Console.WriteLine("Product on {0} and {1} is {2}", a, b, ret);

            Func <int, int, int> func = (x, y) => 3;

            func = (x, y) => x * y;
            func = (x, y) =>
            {
                Console.WriteLine("Dividing integers is not very convenient. Think of using doubles or decimals");
                var result = x / (double)y;
                return((int)result);
            };

            Action <int, int> actionGeneric = ((x, y) =>
            {
                Console.WriteLine(ret);
                Console.WriteLine(operation(x, y));
            });

            Action emptyAction = () => { };

            emptyAction(); // does nothing

            Console.ReadKey();
        }