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:

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

  1. // Create a handler for a click event.
  2. button1.Click += delegate(System.Object o, System.EventArgs e)
  3. { System.Windows.Forms.MessageBox.Show("Click!"); };
  1. // Create a delegate.
  2. delegate void Del(int x);
  3.  
  4. // Instantiate the delegate using an anonymous method.
  5. 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

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

  1. void StartThread()
  2. {
  3. System.Threading.Thread t1 = new System.Threading.Thread
  4. (delegate()
  5. {
  6. System.Console.Write("Hello, ");
  7. System.Console.WriteLine("World!");
  8. });
  9. t1.Start();
  10. }

C#入门经典的解释:

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

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

  1. delegate(parameters)
  2.  
  3. {
  4.  
  5. //Anonymous method code
  6.  
  7. };

其中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. 牛客网_Wannafly模拟赛1

    A.矩阵 题目链接:https://www.nowcoder.com/acm/contest/submit/f8363c912a4c48a28b80f47e7102b6b8?ACMContestId= ...

  2. [已解决]centos6.4 php连接mysql和memcache提示权限不允许

    昨天新安装了64位系统,所以就重新安装了一个centos的虚拟机,配置了一个测试环境,刚开始是页面打不开,这个确定是selinux规则没有设置好使用如下命令解决: chcon -R -h -t htt ...

  3. 关于spring的applicationContext.xml配置文件的ref和value之自我想法

    今天在做SSH的一个项目的时候,因为需要定时操作,所以就再sping里面加入了一个quartz的小定时框架,结果在运行时候,发生了一个小bug. Caused by: org.springframew ...

  4. java获取时间段内的所有日期

        public static void main(String[] args) {        SimpleDateFormat dateFormat = new SimpleDateForm ...

  5. sVIrt概述

    sVirt概述 前面已经对seLInux的基本原理做了分析,seLinux主要就是基于主体和客体的安全上下文,进行访问决策.那么安全上下文是不是又可以理解为一个标签呢? 基于以上seLInux的特性, ...

  6. 18.让sublime text3支持Vue语法高亮显示

    1.准备语法高亮插件vue-syntax-highlight. 下载地址: https://github.com/vuejs/vue-syntax-highlight 下载页面并下载: 解开压缩包vu ...

  7. MySQL,sqlalchemy

    Mariadb 数据库是一堆表的集合 主键 外键 索引 安装: Centos7 [root@host]# mysqladmin -u root password "new_password& ...

  8. Outputting Strings in the Console

    Outputting Strings in the Console #include <windows.h> class Console {public:  enum fore_color ...

  9. WebBrowser自动填充打开文件对话框

    WebBrowser自动填充打开文件对话框   在使用WebBrowser编写自动表单填写软件的时候,不知道大家是否遇到国填写文件选择表单的情况.遇到这种情况的时候,无法直接队Html元素赋值,必须模 ...

  10. 来自IOS开发工程师的零基础自学HTML5经验分享

    移动互联网的火爆,而Html具有跨平台.开发快的优势,越来越受到开发者的青睐.感谢IOS开发工程师“小木___Boy”’带来的HTML5学习经验分享. 一.学习途径 1.很多视频网站 比如慕课.和极客 ...