匿名方法和Lambda表达式

出于MVVM学习的需要,复习下匿名方法和Lambda表达式,因为之前用的也比较少,所以用的也不是很熟练,Baidu下相关的知识,写了这个Demo,目标是用简单的方法展示这个怎么用。

这里偏重的和LINQ中的Lambda表达式 

var filesLookup = files.ToLookup(f=>f.Substring(f.LastIndexOf('.')).ToUpper());

有些不同,两种不同的地方使用吧。
DebugLZQ把要写的话,都写在Demo的注释中了,相关类似的东西网上很多,这里就不墨迹了,如下:

using System;namespace 匿名函数和Lambda表达式
{class Program{/// <summary>/// Func和Action类是特殊的类型,它们允许你在不必指定自定义委托类型的情况下,去使用委托。在整个.NET框架中都可以使用它们。/// /// Lambda表达式已经广泛代替了匿名方法,因为它是从C# 3.0开始引入的。Lambda表达式与匿名方法有很多同样的功能,但更便于使用。/// </summary>/// <param name="args"></param>static void Main(string[] args){//Action委托封装不返回结果的方法。换句话说,这种委托只能用于以void关键字定义的那些方法。Action<int,int> funa = delegate(int x, int y) { Console.WriteLine(x+y); };//匿名方法
Action<int, int> funb = (int x, int y) => { int z = x + y; Console.WriteLine(z); };//Lambda表达式//输出funa(3, 4);funb(3, 4);Console.ReadKey();//System.Func委托除了可以返回结果以外,它与Action委托完全相同。最简单的Func实现没有参数。Func<int, int, int> fun1 = delegate(int x, int y) { return x * y; };//匿名方法
Func<int, int, int> fun2 = (int x, int y) => { return x * y; };//Lambda表达式//输出Console.WriteLine(fun1(3, 4));Console.WriteLine(fun2(3, 4));Console.ReadKey();            }}
}

一图胜过千言,程序运行如下:

 

 

转载于:https://www.cnblogs.com/DebugLZQ/archive/2012/12/06/2804929.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377034.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

烂橘子

Problem Statement: 问题陈述&#xff1a; Given a matrix of dimension r*c where each cell in the matrix can have values 0, 1 or 2 which has the following meaning: 给定尺寸r * C的矩阵&#xff0c;其中矩阵中的每个单元可以具有其具有以下含义的值0&#xff0c;1或2…

android junit 测试程序

http://blog.csdn.net/to_cm/article/details/5704783 Assert.assertEquals(2, t); 断言转载于:https://www.cnblogs.com/wjw334/p/3714120.html

MySQL 8.0.22执行器源码分析HashJoin —— BuildHashTable函数细节步骤

BuildHashTable函数细节步骤 该函数位置处于hash_join_iterator.cc 403 ~ 560行 step1&#xff1a;如果被驱动表迭代器没有更多的行数&#xff0c;更新m_state为EOR&#xff0c;然后返回false&#xff0c;表明创建hash表失败 if (!m_build_iterator_has_more_rows) {m_state…

《那些年啊,那些事——一个程序员的奋斗史》——125

距离离职交接的一个月时间还剩几天&#xff0c;本来应该是平淡无事的&#xff0c;却没想到最后还是波澜四起。昨天下班前&#xff0c;公司突然停了电。这本是件普通得不能再普通的事情&#xff0c;可没想到过了一会来电了&#xff0c;或许是波峰电压太大&#xff0c;或许是稳压…

python中的元类_Python中的元类

python中的元类Python元类 (Python metaclass) A metaclass is the class of a class. A class defines how an instance of a class i.e.; an object behaves whilst a metaclass defines how a class behaves. A class is an instance of a metaclass. 元类是类的类。 一个类…

MySQL 8.0.22执行器源码分析HashJoin —— 一些初始化函数的细节步骤

目录InitRowBuffer&#xff08;101行~126行&#xff09;InitProbeIterator&#xff08;142行~153行&#xff09;*HashJoinIterator* 的Init&#xff08;155行~240行&#xff09;InitializeChunkFiles&#xff08;364行~401行&#xff09;InitWritingToProbeRowSavingFile&#…

c语言的宏定义学习笔记

宏定义 在预处理之前&#xff0c;c预处理器会对代码进行翻译&#xff0c;譬如用blank替换注释&#xff0c;去掉多余的空格&#xff0c;删除末尾的\来拼接行等。 例如&#xff1a; int /*注释*/ x; 会被翻译成 int x; printf("this is a s\ entence."); 会被翻译成 pr…

摄氏温度转换华氏温度_什么是摄氏温度?

摄氏温度转换华氏温度摄氏温度 (Celsius) Celsius is a temperature measuring scale which as a SI unit derived from the seven base units stated and described by the International System of Units (SI). 摄氏温度是一种温度测量刻度&#xff0c;它是由国际单位制(SI)所…

别人的算法学习之路

http://www.cnblogs.com/figure9/p/3708351.html 我的算法学习之路 关于 严格来说&#xff0c;本文题目应该是我的数据结构和算法学习之路&#xff0c;但这个写法实在太绕口——况且CS中的算法往往暗指数据结构和算法&#xff08;例如算法导论指的实际上是数据结构和算法导论&a…

git config命令使用第二篇——section操作,多个key值操作,使用正则

接上一篇&#xff0c;git config命令使用第一篇——介绍&#xff0c;基本操作&#xff0c;增删改查:http://blog.csdn.net/hutaoer06051/article/details/8275069 1. 删除一个section 命令参数 --remove-section 格式&#xff1a;git config [--local|--global|--system] --rem…

MySQL面试准备——64页pdf

本笔记为以前整理的零碎的关于Mysql的知识点&#xff0c;有深入源码的也有浅层的八股。已经被我整理成了一个pdf。 实习岗位正好也是和数据库内核有关的&#xff0c;之后应该还会更新。做个整理&#xff0c;方便秋招的时候快速回顾吧。 链接&#xff1a;链接 提取码&#xff1a…

python点图_Python | 点图

python点图The dot plot is a type of data representation in which each data-point in the figure is represented as a dot. Dot plot underlies discrete functions unlike a continuous function in a line plot. Each value could be correlated but cannot be connecte…

SAP-MM:发票、贷方凭证、事后借记、后续贷记

发票和事后借记 相同点&#xff1a;增加对供应商的应付款 不同点&#xff1a;针对同一订单收货&#xff0c;发票要先于事后借记&#xff08;事后借记是对供应商后期发票金额的补充&#xff09;&#xff1b;发票和金额、订单数量有关系&#xff0c;而事后借记只是订单金额调整的…

Dijkstra for MapReduce (1)

<math xmlns"http://www.w3.org/1998/Math/MathML"><mi>x</mi><mo>,</mo><mi>y</mi><mo>&#x2208;<!-- ∈ --></mo><mi>X</mi> </math> 准备研究一下Dijkstra最短路径算法Hadoop上…

sql的外键约束和主键约束_SQL约束

sql的外键约束和主键约束SQL | 约束条件 (SQL | Constraints) Constraints are the guidelines implemented on the information sections of a table. These are utilized to restrict the kind of information that can go into a table. This guarantees the precision and …

nios pio interrupt 的使能

关于nios 中的中断&#xff0c;因为要16c550中需要nios的中断环境去测试&#xff0c;所以就用到了中断。 硬件&#xff1a;在nios中添加硬件PIO,但是要使能中断功能。如下图所示&#xff1a; 系统列化&#xff0c;PIO的连接就不说了。但是要注意两地方&#xff1a;edge type&am…

《单线程的build hash table、write rows to chunks、hash join的步骤以及流程图》

Build Hash Table流程 1、初始化row buffer2、从build input table中读一行3、若读完build input table所有row&#xff0c;返回状态READING_ROW_FROM_PROBE_item4、否则&#xff0c;向hash map中写入一条row5、如果hash map 写入成功&#xff0c;返回2&#xff0c;继续执行6、…

在Scala的溪流

Scala | 流 (Scala | Streams) Stream in Scala is a type of lazy val. It is a lazy val whose elements are evaluated only when they are used in the program. Lazy initialization is a feature of Scala that increases the performance of the program. Scala中的Stre…

适合高速驱动电路的推挽电路

http://www.dzsc.com/data/html/2008-9-10/69023.html 图1是使用NPN/PNP型晶体管的互补推挽电路&#xff0c;适于驱动功率MOSFET的门极。此电路虽然具有门极电流的驱动能力&#xff0c;但射极输出波形不能比输人信号快。 图2是此电路的开关波形。它表示出tf、tr都快&#xff0c…

cholesky分解

接着LU分解继续往下&#xff0c;就会发展出很多相关但是并不完全一样的矩阵分解&#xff0c;最后对于对称正定矩阵&#xff0c;我们则可以给出非常有用的cholesky分解。这些分解的来源就在于矩阵本身存在的特殊的 结构。对于矩阵A&#xff0c;如果没有任何的特殊结构&#xff0…