zoukankan      html  css  js  c++  java
  • LAMDA表达式学习

    1. 普通绑定:

    public void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show("ok");
            }
    
    this.button1.Click += button1_Click;

    2. 匿名委托:

    this.button1.Click += delegate(object sender, EventArgs e)
                {
                    MessageBox.Show("Click");
                };

    3. LAMDA表达式:

    this.button1.Click += ((sender, e) =>
                {
                    MessageBox.Show("Click");
                }
                    );

    编译器会自动推算出表达式中的类型。

    4. 过滤条件:

    List<User> users = new List<User>();
    
                Func<User, bool> predicate = (
                    (user) =>
                    {
                        return user.UserId > 100;
                    }
                    );
                List<User> temps = users.Where(predicate).ToList();

    等同于:

    List<User> temps = users.Where(p => p.UserId > 100).ToList();
    List<User> temps = (from p in users where p.UserId > 100 select p).ToList();
    List<User> users = new List<User>();
                Func<User, bool> predicate = (
                    (user) => user.UserId > 100
                    );
                List<User> temps = users.Where(predicate).ToList();

    单挑语句时不需要使用{},同时可以不使用return来返回结果。

    5. 排序:

    List<User> users = new List<User>();
                List<User> temp1 = users.OrderBy(p=>p.UserId).ToList();
                Func<User, int> orderby = (user => user.UserId);
                List<User> temp2 = users.OrderBy(orderby).ToList();
  • 相关阅读:
    Form组件
    LAMP+Varnish的实现
    缓存反向代理-Varnish
    CDN初识
    HTTP缓存初探
    Keepalived搭建主从架构、主主架构实例
    实现高可用-Keepalived
    nginx负载均衡实例
    lvs集群实现lvs-dr模型和lvs-nat模型
    LVS介绍
  • 原文地址:https://www.cnblogs.com/TeyGao/p/2638859.html
Copyright © 2011-2022 走看看