C# Programming Guide

Anonymous Functions

Lambda Expressions

Anonymous Methods

In versions of C# before 2.0, the only way to declare a delegate was to use named methods.

在C#2.0以前,声明委托的唯一方式就是使用命名方法。

C# 2.0 introduced anonymous methods and in C# 3.0 and later, lambda expressions supersede anonymous methods as the preferred way to write inline code.

在C#2.0中介绍了匿名方法,在C#3.0以及之后的版本中,lambda表达式取代了匿名方法,成为编写内联代码的首选。

However, the information about anonymous methods in this topic also applies to lambda expressions.

然而,本主题关于匿名方法的信息同样使用与lambda表达式

There is one case in which an anonymous method provides functionality not found in lambda expressions.

某种情况下,匿名方法能提供lambda表达式所不具备的功能

Anonymous methods enable you to omit the parameter list.

匿名方法确保你可以使用参数忽略列表

This means that an anonymous method can be converted to delegates with a variety of signatures.

这意味着,匿名方法可以被转换为具有各种签名的委托。

This is not possible with lambda expressions.

对于lambda表达式而言,这是不可能的。

Creating anonymous methods is essentially a way to pass a code block as a delegate parameter. Here are two examples:

创建匿名方法实质上是一种"将代码块作为委托参数进行传递"的方式。这里有两个示例:

// Create a handler for a click event.
button1.Click += delegate(System.Object o, System.EventArgs e)
{ System.Windows.Forms.MessageBox.Show("Click!"); };
// Create a delegate.
delegate void Del(int x); // Instantiate the delegate using an anonymous method.
Del d = delegate(int k) { /* ... */ };

By using anonymous methods, you reduce the coding overhead in instantiating delegates because you do not have to create a separate method.

通过使用匿名方法,你可以降低实例化委托的编码开销,因为你无须创建一个单独的方法。

For example, specifying a code block instead of a delegate can be useful in a situation when having to create a method might seem an unnecessary overhead.

比如,指定代码块而不是委托,当创建一个方法可能造成不必要的开销的时候很有用。

A good example would be when you start a new thread. This class creates a thread and also contains the code that the thread executes without creating an additional method for the delegate

开启一个新线程会是一个很好的例子。该类创建了一个线程,并且包含了线程将要执行的代码,还不需要为委托创建额外的方法。

void StartThread()
{
System.Threading.Thread t1 = new System.Threading.Thread
(delegate()
{
System.Console.Write("Hello, ");
System.Console.WriteLine("World!");
});
t1.Start();
}

C#入门经典的解释:

除了定义事件处理方法之外,还可以使用匿名方法。匿名方法实际上是传统意义上不存在的方法,它不是某一个类的方法,而纯粹是用作委托目的而创建的。

要创建匿名方法,需要使用下面的代码:

delegate(parameters)

{

//Anonymous method code

};

其中parameters是一个参数列表,这些参数匹配正在实例化的委托类型,由匿名方法的代码使用,例如

delegate(Connection source,MessageArrivedEventArgs e)

{

//Anonymous method code matching MessageHandler event in Ch13Ex03

}

使用这段代码就可以完全绕过Ch13Ex03中的DisplayMessage方法:

myConnection1.MessageArrived+=

delegate(Connection source,MessageArrivedEventArgs e)

{

//code

}

对于匿名方法要注意,对于包含它们的代码块来说,它们是局部的,可以访问这个区域内的局部变量。如果使用这样一个变量,它就称为外部变量。

外部变量在超出作用域时,是不会删除的,这与其他的局部变量不同,在使用它们的匿名方法被释放时,外部变量才会删除。

这比我们希望的时间晚一些,所以要格外小心。

C# Programming Guide-->Statements, Expressions, and Operators-->Anonymous Functions的更多相关文章

  1. [IoLanguage]Io Programming Guide[转]

    Io Programming Guide     Introduction Perspective Getting Started Downloading Installing Binaries Ru ...

  2. Extension Methods (C# Programming Guide)

    https://msdn.microsoft.com/en-us//library/bb383977.aspx private static void Dump(this ArraySegment&l ...

  3. Structured Streaming编程 Programming Guide

    Structured Streaming编程 Programming Guide Overview Quick Example Programming Model Basic Concepts Han ...

  4. Flink DataSet API Programming Guide

     https://ci.apache.org/projects/flink/flink-docs-release-0.10/apis/programming_guide.html   Example ...

  5. 对Spark2.2.0文档的学习3-Spark Programming Guide

    Spark Programming Guide Link:http://spark.apache.org/docs/2.2.0/rdd-programming-guide.html 每个Spark A ...

  6. Spark Streaming Programming Guide

    参考,http://spark.incubator.apache.org/docs/latest/streaming-programming-guide.html Overview SparkStre ...

  7. Apache Spark 2.2.0 中文文档 - GraphX Programming Guide | ApacheCN

    GraphX Programming Guide 概述 入门 属性 Graph 示例属性 Graph Graph 运算符 运算符的汇总表 Property 运算符 Structural 运算符 Joi ...

  8. Interfaces (C# Programming Guide)

    https://msdn.microsoft.com/en-us/library/ms173156.aspx An interface contains definitions for a group ...

  9. 串口通信编程向导 Serial Programming Guide for POSIX Operating Systems

    https://www.cmrr.umn.edu/~strupp/serial.html#CONTENTS Introduction Chapter 1, Basics of Serial Commu ...

  10. 【IOS笔记】View Programming Guide for iOS -1

    原文:View Programming Guide for iOS View and Window Architecture Views and windows present your applic ...

随机推荐

  1. php和jsCOOKIE实现前端交互

    w如何精简? <script> document.cookie = 'wjs_cookie=' + 'amz_reviews'; function w(id) { document.coo ...

  2. sql语句的安全性考虑

    sql语句的应该考虑哪些安全性呢? 1.防止sql注入,对特殊字符进行转义(addslashes),或者使用已经编译好的sql语句进行变量的绑定: 2.当sql运行出现错误的时候,不要把数据库返回的错 ...

  3. django将数据库中数据直接传到html

    1.当然,前提是建立和配置好django数据库啦~ 2.在python后台函数中引入需要的表 #要读取home这个APP下的models.py文件,引入其中的Student_message_unedi ...

  4. (1.3.1)连接安全(连接实例与网络协议及TDS端点)

    连接安全是sql server安全配置的第1道防线,它保证只有许可的客户端能够连接sql server,而且可以限制连接可用的通道(各种网络协议). 1.连接到sql server实例 sql ser ...

  5. 汉字转换为拼音的JavaScript库

    将JSPinyin剥离mootools这个JavaScript库,可以独立使用. 1)一个是将汉字翻译为拼音,其中每一个字的首字母大写: pinyin.getFullChars(this.value) ...

  6. 如何保护自己的GitHub代码不被别人覆盖

    我们在自己的github上创建了免费的公开代码,为了防止别人通过git push upstream master 覆盖了自己原有的代码,需要作一下设置:Settings->Branchs,然后在 ...

  7. Java将数据写进excel

    Java将数据写进excel Java将数据写进excel class User { private String name ; private String password; public Use ...

  8. XE6移动开发环境搭建之IOS篇(2):安装虚拟机(有图有真相)

    XE6移动开发环境搭建之IOS篇(2):安装虚拟机(有图有真相) 2014-08-15 22:04 网上能找到的关于Delphi XE系列的移动开发环境的相关文章甚少,本文尽量以详细的内容.傻瓜式的表 ...

  9. SQL Server 使用 Pivot 和 UnPivot 实现行列转换

    对于行列转换的数据,通常也就是在做报表的时候用的比较多,之前也零零散散的看了一些,今天就来总结一下. 先创建一个用于演示的临时表: create table #temp ( 年份 ) null, 月份 ...

  10. React:快速上手(7)——使用中间件实现异步操作

    React:快速上手(7)——使用中间件实现异步操作 本文参考链接:Stack Overflow redux-thunk 我们使用store.dispath进行派发时,只能传递一个普通对象进去,如下: ...