【winfrom】委托Lambda

 delegate void Cal();
    public partial class Form1 : Form
    {

        //委托 
        //2.0之前  命名方法
        //2.0引用  匿名方法
        //3.0 Lambda  简化匿名方法 
        //  委托   匿名方法 Lambda
        // 委托 是一个类,定义方法的类型,将一个方法当作另一个方法的参数来进行传递
        // 类 引用类型

        event Caluate CalEvent; 
        delegate int Caluate(int a, int b);//定义一个委托
        public Form1()
        {
            InitializeComponent();
        }
        int sum = 4;
       
       //加
        private int Add(int x, int y)
        {
            return sum += x + y;
        }
        //减
        private int Subtract(int x, int y)
        {
            return sum += x - y;
        }
        //乘
        private int Multiply(int x, int y)
        {
            return sum += x * y;
        }



        private void Form1_Load(object sender, EventArgs e)
        {
            Caluate cal = new Caluate(Add);//委托的实例化
            //Caluate cal1 = Subtract;//赋值委托
            cal += Subtract;
            cal += Multiply;    //多播委托,执行是按添加的顺序来执行

            cal -= Subtract;//方法的移除

            int re = cal.Invoke(3, 5);
            int re1 = cal(3, 5);


            //匿名方法
            Caluate cal2 = delegate(int x, int y)
            {
                return x + y;
            };

            cal2 += delegate(int m, int n)
            {
                return m * n;
            };

            int re3 = cal2.Invoke(4, 9);//36
                                            //goes to

            Caluate cal3 = (int x, int y) => { return x + y; };//Lambda表达式

            Caluate cal4 = (x,y) =>  x + y; //Lambda表达式 简化

            Cal c = () => { Console.WriteLine("aaaa"); };

           //Action 不提供返回值,参数0,1,2.。。。16个参数

            Action act = () => { Console.WriteLine("Hello,Welcome!"); };
            Action<string> act1 = s => { Console.WriteLine("Hello,{0}", s); };
            Action<string,int> act2 = (s,t) => { Console.WriteLine("Hello,{0},{1}", s,t); };


            //Func 只提供一个返回值,参数0,1,2.。。。16个参数

            Func<string> func1=()=>"ssss";
            Func<string, string> func2 = s => s+"ssss";

            Func<string, int, string> func3 = (s, i) =>
                {
                    Console.WriteLine("sssssddddd");
                    return s + "ssss" + i;
                };




            List<User> list = new List<User>();
            list.Add(new User() { Id = 1, UserName = "小红", Age = 22 });
            list.Add(new User() { Id = 1, UserName = "路人", Age = 20});
            list.Add(new User() { Id = 1, UserName = "Jonathan", Age = 24 });
            list.Add(new User() { Id = 1, UserName = "Telnet", Age = 26 });
            list.Add(new User() { Id = 1, UserName = "陈小哥", Age = 21 });
            list.Add(new User() { Id = 1, UserName = "anymore", Age = 25 });
            list.Add(new User() { Id = 1, UserName = "LTE-ZHE", Age = 23 });
            list.Add(new User() { Id = 1, UserName = "Antidote", Age = 27 });
            list.Add(new User() { Id = 1, UserName = "Jerry", Age = 30 });
            list.Add(new User() { Id = 1, UserName = "Lily", Age = 29 });

            //扩展方法  定义在静态类中的静态方法

            string ss = "234";
            int iSS = ss.ToInt();


            list.Where(u => u.Age > 24);
            list.OrderBy(u => u.Id);
            list.OrderByDescending(u => u.Id);

        }

  

 public class User
    {
        public int Id { get; set; }
        public string UserName { get; set; }
        public int Age { get; set; }
    }

  

    public static class Converter
    {
        public static int ToInt(this string str)
        {
            int reInt = 0;
            int.TryParse(str, out reInt);
            return reInt;
        }
    }

  

posted @ 2018-03-26 09:11  Microera  阅读(227)  评论(0)    收藏  举报