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表达式就是一个无参数的委托及具体方法的组合体,这是一个常规的套路,可以直接记住。

  • 相关阅读:
    CentOS 6.3下Samba服务器的安装与配置(转)
    利用香蕉派自制电视盒子
    利用arduino制作瓦力万年历-1.0
    arduino:int & double 转string 适合12864下使用
    centos 6.X下建立arduino开发环境
    树莓派学习笔记(7):利用bypy实现树莓派NAS同步百度云
    直接插入排序
    直接选择排序
    快速排序算法
    git 分支管理 推送本地分支到远程分支等
  • 原文地址:https://www.cnblogs.com/liuzhendong/p/3687683.html
Copyright © 2011-2022 走看看