首页 > 其他 > 详细

Lambda表达式

时间:2015-10-25 13:42:26      阅读:204      评论:0      收藏:0      [点我收藏+]
class Program
{
    static void Main(string[] args)
    {
        //Lambda表达式输出List集合每一项
        List<string> list = new List<string>() { "aaa", "bbb", "ccc", "ddd", "eee" };
        list.ForEach(d => Console.WriteLine(d));

        //Lambda表达式查找匹配元素
        List<A1> a1 = new List<A1>();
        a1.Add(new A1 { Id = 10, Name = "aaa" });
        a1.Add(new A1 { Id = 11, Name = "bbb" });
        a1.Add(new A1 { Id = 12, Name = "ccc" });
        a1.Add(new A1 { Id = 13, Name = "ddd" });
        List<A1> list1 = a1.FindAll(d => d.Id > 11);
        //Lambda排序
        a1.Sort((x, y) => y.Id - x.Id);

        //Lambda匹配指定元素到新集合
        var s1 = a1.Select(d => new A2() { Name = d.Name });
        foreach (var item in s1)
        {
            Console.WriteLine(item.Name);
        }

        //Lambda演变过程
        var gg = new Func<string, int>(delegate(string str) { return str.Length; });
        int s = gg("abcde");
        var ss = delegate(string str) { return str.Length; };
        var qq = (string str) => { return str.Length; };
        var ww = (string str) => str.Length;
        var ee = str => str.Length;
        var bb = new Func<int, int, int>(delegate(int x, int y) { return x + y; });
        int cc = bb(5,10);
        Console.ReadKey();
    }
}
public class A1
{
    public int Id { get; set; }
    public string Name { get; set; }
}
public class A2
{
    public string Name { get; set; }
}

 

Lambda表达式

原文:http://www.cnblogs.com/genesis/p/4908612.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!