c# Lambda表达式和表达式树

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SRLJCSHAP.委托.Demo
{
    delegate void StringProcessor(string input);//声明委托类型
    public class Person
    {
        string name;
        public Person(string Name)
        {
            this.name = Name;
        }
        //声明兼容的实例方法
        public void Say(string Msg)
        {
            Console.WriteLine("{0} says: {1}", name, Msg);
        }


    }
    public class Background
    {
        //声明兼容的静态方法
        public static void Note(string note)
        {
            Console.WriteLine("({0})", note);
        }

       
    }
}

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Windows.Forms;

namespace SRLJCSHAP.Lambda表达式和表达式树.Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<string, int> returnLength;
            returnLength = text => text.Length;
            Console.WriteLine(returnLength("Hello"));
            #region 用Lambda表达式来处理一个电影列表
            var films = new List<Film>
            {
                new Film{ name="龙在江湖", year=1999},
                new Film{ name="暴力街区",year=2010},
                new Film{ name="大话西游",year=1998},
                new Film{name="南京南京",year=1985},
                new Film{name="The Wizard OF oz",year=1935},
                new Film{name="apple",year=2014}

            };
            Action<Film> print =
                film => Console.WriteLine("Name={0},year={1}", film.name, film.year);//创建可重用的列表打印委托
            films.ForEach(print);//打印原始的列表

            films.FindAll(f => f.year < 1990).ForEach(print);//创建筛选过的列表
            films.Sort((f1, f2) => f1.name.CompareTo(f2.name));//排序原始列表
            films.ForEach(print);
            #endregion
            #region 用Lambda表达式来记录事件
            Button btn = new Button { Text = "Click me" };
            btn.Click += (src, e) => Log("Click", src, e);
            btn.KeyPress += (src, e) => Log("KeyPress", src, e);
            btn.MouseClick += (src, e) => Log("MouseClick", src,e);
            Form frm = new Form { AutoSize = true, Controls = { btn } };
            Application.Run(frm);
            #endregion
            #region 一个非常简单的表达式树
            Expression first = Expression.Constant(2);
            Expression second = Expression.Constant(3);
            Expression add = Expression.Add(first, second);
            Console.WriteLine(add);
            #endregion
            Console.ReadKey();
        }
        static void Log(string title, object sender, EventArgs e)
        {
            Console.WriteLine("Event:{0}", title);
            Console.WriteLine("     Sender{0}", sender);
            Console.WriteLine(" Arguments:{0} ", e.GetType());

            foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(e))
            {
                string name = prop.DisplayName;
                object value = prop.GetValue(e);
                Console.WriteLine("{0}={1}", name, value);
            }
        }
    }
}

 

c# Lambda表达式和表达式树

上一篇:Windows IIS


下一篇:[ruby]Windows Ruby安装步骤