System.Action的使用(lambda 表达式)
对于Action的使用方法使用如下:
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string first = "First";
var action = new Action(() => { Console.WriteLine(first); });
action(); var action2 = new Action<string>((s) => { Console.WriteLine($"Action<T>:{s}"); });
action2(first); var action3 = new Action<string, string>((s1, s2) => {
Console.WriteLine($"Action<T1,T2>:{s1},{s2}");
});
action3(first, "second");
}
}
}
使用dotPeek通过反编译,得到代码:
namespace ConsoleApp1
{
internal class Program
{
private static void Main(string[] args)
{
string first = "First";
((Action) (() => Console.WriteLine(first)))();
((Action<string>) (s => Console.WriteLine(string.Format("Action<T>:{0}", (object) s))))(first);
((Action<string, string>) ((s1, s2) => Console.WriteLine(string.Format("Action<T1,T2>:{0},{1}", (object) s1, (object) s2))))(first, "second");
}
}
}
下面写一种与反编译出来的相似的方法
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string first = "First";
var action = new Action(() => { Console.WriteLine(first); });
action(); var action2 = new Action<string>((s) => { Console.WriteLine($"Action<T>:{s}"); });
action2(first); var action3 = new Action<string, string>((s1, s2) =>
{
Console.WriteLine($"Action<T1,T2>:{s1},{s2}");
});
action3(first, "second"); new Action(() => { Console.WriteLine(first); })();
new Action<string>((s) => { Console.WriteLine($"Action<T>:{s}"); })(first);
new Action<string, string>((s1, s2) =>
{
Console.WriteLine($"Action<T1,T2>:{s1},{s2}");
})(first, "second");
}
}
}
看一下反编译的结果:
namespace ConsoleApp1
{
internal class Program
{
private static void Main(string[] args)
{
string first = "First";
((Action) (() => Console.WriteLine(first)))();
((Action<string>) (s => Console.WriteLine(string.Format("Action<T>:{0}", (object) s))))(first);
((Action<string, string>) ((s1, s2) => Console.WriteLine(string.Format("Action<T1,T2>:{0},{1}", (object) s1, (object) s2))))(first, "second");
((Action) (() => Console.WriteLine(first)))();
string str1 = first;
((Action<string>) (s => Console.WriteLine(string.Format("Action<T>:{0}", (object) s))))(str1);
string str2 = first;
string str3 = "second";
((Action<string, string>) ((s1, s2) => Console.WriteLine(string.Format("Action<T1,T2>:{0},{1}", (object) s1, (object) s2))))(str2, str3);
}
}
}
反编译结果是帮我们定义了几个变量。
System.Action的使用(lambda 表达式)的更多相关文章
- C# delegate event func action 匿名方法 lambda表达式
delegate event action func 匿名方法 lambda表达式 delegate类似c++的函数指针,但是是类型安全的,可以指向多个函数, public delegate void ...
- C#高级功能(一)Lambda 表达式
Lambda 表达式是一种可用于创建委托或表达式目录树类型的匿名函数. 通过使用 lambda 表达式,可以写入可作为参数传递或作为函数调用值返回的本地函数. Lambda 表达式对于编写 LINQ ...
- 24.C# Lambda表达式
1.Lambda表达式的含义 Lambda表达式是C#3.0引入的一种结构,使用它可以简化C#编程. 2.Lambda表达式与匿名方法 我们知道匿名方法可用于事件处理,如下delegate声明了一个匿 ...
- Linq快速入门——Lambda表达式的前世今生
Linq快速入门——Lambda表达式的前世今生 Lambda表达式其实并不陌生,他的前生就是匿名函数,所以要谈Lambda表达式,就不得不谈匿名函数,要谈匿名函数,那又要不得不谈委托. 何为委托 ...
- Java8之熟透Lambda表达式
一.Lambda简述 1.1.Lambda概述 Lambda 表达式可以理解为简洁地表示可传递的匿名函数的一种方式:它没有名称,但它有参数列表.函数主体.返回类型,可能还有一个可以抛出的异常列表. ...
- 夯实Java基础(二十二)——Java8新特性之Lambda表达式
1.前言 Java 8于14年发布到现在已经有5年时间了,经过时间的磨练,毫无疑问,Java 8是继Java 5(发布于2004年)之后的又一个非常最重要的版本.因为Java 8里面出现了非常多新的特 ...
- Java 8新特性-3 Lambda 表达式
在 Java 8 之前,匿名内部类,监听器和事件处理器的使用都显得很冗长,代码可读性很差. 在Java 8 之前使用匿名内部类: 例如 interface ITestPrint{ public voi ...
- 0028 Java学习笔记-面向对象-Lambda表达式
匿名内部类与Lambda表达式示例 下面代码来源于:0027 Java学习笔记-面向对象-(非静态.静态.局部.匿名)内部类 package testpack; public class Test1{ ...
- LinQ—Lambda表达式
概述 本篇博客主要解说lambda表达式,在这里将它的来龙去脉,主要是从托付,匿名函数这些方面过度讲的,当然,在讲托付和匿名函数的时候,主要是从Lambda的角度出发讲的,可能它们还具有其他的一些作用 ...
- Python中lambda表达式学习
lambda只是一个表达式,函数体比def简单很多. lambda的主体是一个表达式,而不是一个代码块.仅仅能在lambda表达式中封装有限的逻辑进去. lambda表达式是起到一个函数速写的作用.允 ...
随机推荐
- linux命令大全(1)
当用户使用linux系统时,其实在和Shell在打交道,当用户发出指令,其实先将这些指令发送给Shell, 然后由Shell将用户的指令翻译后传送给内核,再由内核来控制硬件的工作. 然后内核将硬件的工 ...
- JPanel JScrollPanel
JPanel 和 JScrollPanel 都属于面板,也是 Swing 中间容器,可以作为容器存放组件,但必须被添加到其他容器中. JPanel 可以聚集一些组件来布局, JScrollPanel ...
- JVM 字节码(三)异常在字节码中的处理(catch 和 throws)
JVM 字节码(三)异常在字节码中的处理(catch 和 throws) 在 ClassFile 中到底是如何处理异常的呢? 一.代码块异常 catch catch 中的异常代码块在异常是如何处理的呢 ...
- web版ssh的使用
一.web_ssh版本安装使用 web_ssh源码:https://github.com/shellinabox/shellinabox 1)安装依赖包 yum install git openssl ...
- linux之用户密码破解的操作
一 无引导介质救援模式破解root用户密码 1 启动虚拟用户,在GRUB启动画面停留的那段时间,用上下键选择启动项. 2 用‘e’键进入你选择的启动项 ,然后用上下键将光标移动到“linux16... ...
- Spring的介绍与搭建
一.Spring的介绍 二.Spring的搭建 (1)导包 (2)创建一个对象 (3)书写配置注册对象到容器 (4)代码测试
- selenium_unittest基本框架
from selenium import webdriver import unittest import time #创建类引入unitest.testcase用例库 class BaiDu_tes ...
- MUI的一些笔记
自定义图标 https://www.iconfont.cn选择图标添加入购物车 进入项目管理下载需要的图标压缩包之后按照自己的需求进行html的操作 事件绑定 mui(dom)on( event , ...
- 走进JDK(二)------String
本文基于java8. 基本概念: Jvm 内存中 String 的表示是采用 unicode 编码 UTF-8 是 Unicode 的实现方式之一 一.String定义 public final cl ...
- flask 学习
标题 操作 09-2-sqlalchemy数据库查询 (2019-01-18 23:30) 编辑 09-1-数据库扩展包flask-sqlalchemy (2019-01-18 17:53) 编辑 0 ...