Lambda表达式

/*----------Lambda
             * */
            //--如果仅有一个入参,则可省略圆括号。
            Console.WriteLine("*************************************");
            Action dummyLambda = () =>
                 {
                     Console.WriteLine("Hello World from a Lambda expression!");
                 };
            dummyLambda();

            //--如果仅有一行语句,并且在该语句中返回,则可省略大括号,并且也可以省略 return 关键字。
            Func<double, double> square = x => x * x;
            double douResult = square(5);
            Console.WriteLine(douResult);

            Func<double, double, double> product = (x, y) => x * y;
            double proResult = product(5, 6);
            Console.WriteLine(proResult);

            Action<double, double> printProduct = (x, y) => { Console.WriteLine(x * y); };
            printProduct(5, 9);


            Func<double[], double[], double> dotProduct = (x, y) =>
            {
                var dim = Math.Min(x.Length, y.Length);
                var sum = 0.0;
                for (var i = 0; i != dim; i++)
                    sum += x[i] + y[i];
                return sum;
            };
            double dotResult = dotProduct(new double[] { 1, 2, 3 }, new double[] { 4, 5, 6 });
            Console.WriteLine(dotResult);

 

posted @ 2014-08-11 16:44  编程猴子  阅读(176)  评论(0编辑  收藏  举报