C# Programming Guide-->Statements, Expressions, and Operators-->Anonymous Functions
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的更多相关文章
- [IoLanguage]Io Programming Guide[转]
Io Programming Guide Introduction Perspective Getting Started Downloading Installing Binaries Ru ...
- Extension Methods (C# Programming Guide)
https://msdn.microsoft.com/en-us//library/bb383977.aspx private static void Dump(this ArraySegment&l ...
- Structured Streaming编程 Programming Guide
Structured Streaming编程 Programming Guide Overview Quick Example Programming Model Basic Concepts Han ...
- Flink DataSet API Programming Guide
https://ci.apache.org/projects/flink/flink-docs-release-0.10/apis/programming_guide.html Example ...
- 对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 ...
- Spark Streaming Programming Guide
参考,http://spark.incubator.apache.org/docs/latest/streaming-programming-guide.html Overview SparkStre ...
- Apache Spark 2.2.0 中文文档 - GraphX Programming Guide | ApacheCN
GraphX Programming Guide 概述 入门 属性 Graph 示例属性 Graph Graph 运算符 运算符的汇总表 Property 运算符 Structural 运算符 Joi ...
- Interfaces (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/ms173156.aspx An interface contains definitions for a group ...
- 串口通信编程向导 Serial Programming Guide for POSIX Operating Systems
https://www.cmrr.umn.edu/~strupp/serial.html#CONTENTS Introduction Chapter 1, Basics of Serial Commu ...
- 【IOS笔记】View Programming Guide for iOS -1
原文:View Programming Guide for iOS View and Window Architecture Views and windows present your applic ...
随机推荐
- Microsoft Excel 标题栏或首行锁定
Microsoft Excel 标题栏或首行锁定 在进行Excel编辑的时候,希望在浏览的时候,第一行或者第一列能够始终显示. 需要做的是:在Excel中选择 "视图"->& ...
- Ajax 常用资源
regular online:http://regex.larsolavtorvik.com/ json online:http://json.cn/ Prototype:http://prototy ...
- 页面加载异常 清除浏览器静态文件 js css 缓存 js动态加载js css文件,可以配置文件后辍,防止浏览器缓存
js清除浏览器缓存的几种方法 - 兔老霸夏 - 博客园 https://www.cnblogs.com/Mr-Rocker/p/6031096.html js清除浏览器缓存的几种方法 一.CSS和 ...
- 前端开发 - CSS - 下
CSS: 12.display 13.浮动效果 14.浮动特性 15.浮动产生的问题和解决方法 16.float京东导航栏 17.position 18.z-index 19.京东案例 12.disp ...
- Java 之 JUC
1. JUC 简介 在 Java 5.0 提供了 java.util.concurrent(简称JUC)包,在此包中增加了在并发编程中很常用的工具类, 用于定义类似于线程的自定义子系统,包括线程池,异 ...
- windows平台tensorboard的配置及使用
由于官网和其他教程里面都是以Linux为平台演示tensorboard使用的,而在Windows上与Linux上会有一些差别,因此我将学习的过程记录下来与大家分享(基于tensorflow1.2.1版 ...
- centos7部署PaaS平台环境(mesos+marathon)
假如有5台主机可以使用,ip地址如下 规划(2master+3slave) master: 192.168.248.205 ---master1 192.168.248.206 ---master2 ...
- MySQL学习之——锁(转)
锁,在现实生活中是为我们想要隐藏于外界所使用的一种工具.在计算机中,是协调多个进程或县城并发访问某一资源的一种机制.在数据库当中,除了传统的计算资源(CPU.RAM.I/O等等)的争用之外,数据也是一 ...
- python 之操作redis数据库(非关系型数据库,k-v)
数据库: 1. 关系型数据库 表结构 2. 非关系型数据库 nosql (k - v 速度快),常用的时以下三种: memcache 存在内存里 redis 存在内存里 mangodb 数据还是存在磁 ...
- Problem A. Array Factory XVII Open Cup named after E.V. Pankratiev Stage 4: Grand Prix of SPb, Sunday, Octorber 9, 2016
思路: 直接二分长度不可行,因为有负数. 考虑枚举坐便删l个数,那如果可以在短时间内求出符合条件的右边最小删的数的个数,这题便可做了. 即:当左边删l个数时,要使sum[n]-sum[l]-fsum[ ...