Fork me on GitHub

C#之 Lambda表达式

Lambda表达式 简化了匿名委托的使用,让你让代码更加简洁,优雅。据说它是微软自c#1.0后新增的最重要的功能之一。

首先来看一下其发展

  

  根据上面的发展历程,可以感到Lambda表达式愈加简化。

详细介绍:

  

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lambda
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 0;
            Func<int, int> f1 = x => 2 * x + 1;
            Func<int, int, bool> f2 = (x, y) => x > y;
            Func<string, int, string> f3 = (x, y) => x.Substring(y);
            Func<int, int> f4 = (x) => sum += x;
            Action a1 = () => { System.Console.WriteLine("HelloWorld"); };
            Action <int> a2 = (x) => { System.Console.WriteLine(x); };
            Action <bool> a3 = (x) => { System.Console.WriteLine(x); };
            Action<string> a4 = (x) => { System.Console.WriteLine(x); };

            for (int i = 1; i <= 10; i++ )
            {
                f4(i);
            }
            a2(sum);
            a1();
            a2(f1(1));
            a3(f2(3, 5));
            a4(f3("Zhengpengfei", 5));
            System.Console.Read();
        }
    }
}

posted @ 2016-06-13 14:45  伊甸一点  阅读(1348)  评论(0编辑  收藏  举报