Exemplo n.º 1
0
        static void Main(string[] args)
        {
            SimpleMath sm = new SimpleMath();

            sm.SetMathMessage((string message, int result) => Console.WriteLine("Message: {0}, Result: {1}", message, result));
            sm.Add(1, 5);
            VerySimpleDelegate d = new VerySimpleDelegate(() => { return("Simple sting"); });

            Console.WriteLine(d.Invoke());

            Console.ReadLine();
        }
Exemplo n.º 2
0
        private static void Main(string[] args)
        {
            var m = new SimpleMath();

            m.SetMathHandler((msg, result) => { Console.WriteLine("Message: {0}, Result: {1}", msg, result); });
            m.Add(10, 10);

            var d = new VerySimpleDelegate(() => { return("Enjoy your string!"); });

            Console.WriteLine(d());

            Console.ReadLine();
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            var simple = new Simple();

            simple.SetMessageHandler((message, result) => { Console.WriteLine($"{message}{result}"); });
            simple.Add(10, 20);

            simple.SetNoParameterDelegate(() => { Console.WriteLine("No parameter delegate"); });
            simple.NoParameter();

            VerySimpleDelegate d = new VerySimpleDelegate(() => { return("Enjoy your string!"); });

            Console.WriteLine(d());
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            VerySimpleDelegate d = new VerySimpleDelegate(() => { return("Enjoy your string"); });
            //Register with delegate as a lambda expression
            SimpleMath m = new SimpleMath();

            m.SetMathHandler((msg, result) =>
            {
                Console.WriteLine($"Message {msg}, Result: {result}");
            });

            //This will execute the lambda expression
            m.Add(10, 10);
            Console.WriteLine(d());
            Console.Read();
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            SimpleMath m = new SimpleMath();

            //使用Lambda表达式与有多个参数的委托交互
            m.SetMathHandler((msg, result) =>// (string msg,int result)=>
            {
                Console.WriteLine("Message: {0},Result: {1}", msg, result);
            });
            m.Add(10, 10);
            //与无参数的委托交互可以这样
            VerySimpleDelegate d = new VerySimpleDelegate(() => { return("Enjoy your string!"); });

            Console.WriteLine(d.Invoke());

            Console.ReadLine();
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Lambdas with Multiple or no Params! *****\n");

            // Register w/ delegate as a lambda expression.
            SimpleMath m = new SimpleMath();

            m.SetMathHandler((msg, result) => { Console.WriteLine("Message: {0}, Result: {1}", msg, result); });

            // This will execute the lambda expression.
            m.Add(10, 10);

            VerySimpleDelegate d = new VerySimpleDelegate(() => { return("Enjoy your string!"); });

            Console.WriteLine(d.Invoke());
            Console.ReadLine();
        }
Exemplo n.º 7
0
        static void LambdaExample()
        {
            SimpleMathLambda m = new SimpleMathLambda();

            m.SetMathHandler((msg, result) =>
            {
                Console.WriteLine("Message: {0}, Result: {1}", msg, result);
            });

            m.Add(10, 10);

            VerySimpleDelegate d = new VerySimpleDelegate(() =>
            {
                return("Enjoy your string!");
            });

            Console.WriteLine(d());
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            //  Register with delegate as a lambda expression.
            SimpleMath m = new SimpleMath();

            m.SetMathHandler((msg, result) =>
            {
                Console.WriteLine("Message: {0}, Result: {1}", msg, result);
            });

            //  This will execute the lambda expression.
            m.Add(10, 10);

            //  Prints "Enjoy your string!" to the console.
            VerySimpleDelegate d = new VerySimpleDelegate(() =>
                                                          { return("Enjoy your string!"); });

            Console.WriteLine("Result from zero param delegate: {0}", d());
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Lambdas *****\n");
            TraditionalDelegateSyntax();
            AnonymousMethodSyntax();
            Console.WriteLine();

            LambdaExpressionSyntax();

            // Register with delegate as a lambda expression.
            SimpleMath m = new SimpleMath();

            m.SetMathHandler((msg, result) =>
            {
                Console.WriteLine("Message: {0}, Result: {1}", msg, result);
            });

            // This will execute the lambda expression.
            m.Add(10, 10);

            VerySimpleDelegate d = new VerySimpleDelegate(() =>
            {
                return("Enjoy your string!");
            });

            Console.WriteLine(d());

            VerySimpleDelegate d2 = new VerySimpleDelegate(() => "Enjoy your string!");

            Console.WriteLine(d2());

            VerySimpleDelegate d3 = () => "Enjoy your string!";

            Console.WriteLine(d3());
            Console.ReadLine();
        }