zoukankan      html  css  js  c++  java
  • 委托到Lambda的进化: ()=> {} 这个lambda表达式就是一个无参数的委托及具体方法的组合体。

    1.原始的委托 (.net 1.0)

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    
    namespace WindowsFormsAppLINQ
    {
        public partial class Form1 : Form
        {
            public delegate void MyDelegate();
            public Form1()
            {
                InitializeComponent();
    
                MyDelegate myDelegate = new MyDelegate(DoSomething);
    
                myDelegate();
    
            }
    
            public void DoSomething()
            {
                MessageBox.Show("Hello");
            }
        }
    }

    2.Action预定义委托, 节省了委托的定义.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsAppLINQ
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
    
                Action myDelegate = new Action(DoSomething);
    
                myDelegate();
            }
    
            public void DoSomething()
            {
                MessageBox.Show("Hello"); 
            }
        }
    }


    3.Lambda表达式, 再省掉方法定义.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsAppLINQ
    {
        public partial class Form3 : Form
        {
            public Form3()
            {
                InitializeComponent();
    
                Action myDelegate = new Action(() => { MessageBox.Show("Hello"); });
    
                myDelegate();
            }       
        }
    }


    4.一对小括号, 直接执行.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsAppLINQ
    {
        public partial class Form4 : Form
        {
            public Form4()
            {
                InitializeComponent();
    
                new Action(() => { MessageBox.Show("Hello"); })();
            }
        }
    }

     总结,()=> {} 这个lambda表达式就是一个无参数的委托及具体方法的组合体,这是一个常规的套路,可以直接记住。

  • 相关阅读:
    hadoop中常见的问题
    RedHat中敲sh-copy-id命令报错:-bash: ssh-copy-id: command not found
    【POJ2411】Mondriaan's Dream(轮廓线DP)
    【CF248E】Piglet's Birthday(动态规划)
    【BZOJ2655】Calc(拉格朗日插值,动态规划)
    【Luogu4781】【模板】拉格朗日插值
    【CF995F】Cowmpany Cowmpensation(动态规划,拉格朗日插值)
    拉格朗日插值公式
    求集合中选一个数与当前值进行位运算的max
    【HDU4471】Homework(矩阵快速幂)
  • 原文地址:https://www.cnblogs.com/liuzhendong/p/3687683.html
Copyright © 2011-2022 走看看