博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
匿名方法
阅读量:7240 次
发布时间:2019-06-29

本文共 2459 字,大约阅读时间需要 8 分钟。

 
此文章由机器翻译。 将光标移到文章的句子上,以查看原文。 
译文
原文

匿名方法(C# 编程指南)

Visual Studio 2012
 
其他版本
 
2(共 2)对本文的评价是有帮助 
 

 

在 2.0 之前的 C# 版本中,声明的唯一方法是使用。 C# 2.0 引入了匿名方法,而在 C# 3.0 及更高版本中,Lambda 表达式取代了匿名方法,作为编写内联代码的首选方式。 不过,本主题中有关匿名方法的信息同样也适用于 Lambda 表达式。 有一种情况下,匿名方法提供了 Lambda 表达式中所没有的功能。 您可使用匿名方法来忽略参数列表。 这意味着匿名方法可转换为具有各种签名的委托。 这对于 Lambda 表达式来说是不可能的。 有关 lambda 表达式的更多特定信息,请参见 。

要将代码块传递为委托参数,创建匿名方法则是唯一的方法。 这里是两个示例:

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

通过使用匿名方法,由于您不必创建单独的方法,因此减少了实例化委托所需的编码系统开销。

例如,如果创建方法所需的系统开销是不必要的,则指定代码块(而不是委托)可能非常有用。 启动新线程即是一个很好的示例。 无需为委托创建更多方法,线程类即可创建一个线程并且包含该线程执行的代码。

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

匿名方法的参数的范围是“匿名方法块”。

如果目标在块外部,那么,在匿名方法块内使用跳转语句(如 、 或 )是错误的。 如果目标在块内部,在匿名方法块外部使用跳转语句(如 gotobreak 或 continue)也是错误的。

如果局部变量和参数的范围包含匿名方法声明,则该局部变量和参数称为该匿名方法的“外部”变量。 例如,下面代码段中的 n 即是一个外部变量:

C#
 
int n = 0;Del d = delegate() { System.Console.WriteLine("Copy #:{0}", ++n); };

外部变量的引用n被认为是捕获在创建委托时。 与本地变量不同,捕获的变量的生存期内扩展,直到引用该匿名方法委托被垃圾回收。

匿名方法不能访问外部范围的  或  参数。

在“匿名方法块”中不能访问任何不安全代码。

在  运算符的左侧不允许使用匿名方法。

 

下面的示例演示实例化委托的两种方法:

  • 使委托与匿名方法关联。

  • 使委托与命名方法 (DoWork) 关联。

两种方法都会在调用委托时显示一条消息。

C#
 
// Declare a delegate.delegate void Printer(string s);class TestClass{    static void Main()    {        // Instatiate the delegate type using an anonymous method.        Printer p = delegate(string j)        {            System.Console.WriteLine(j);        };        // Results from the anonymous delegate call.        p("The delegate using the anonymous method is called.");        // The delegate instantiation using a named method "DoWork".        p = new Printer(TestClass.DoWork);        // Results from the old style delegate call.        p("The delegate using the named method is called.");    }    // The method associated with the named delegate.    static void DoWork(string k)    {        System.Console.WriteLine(k);    }}/* Output:    The delegate using the anonymous method is called.    The delegate using the named method is called.*/
 

参考

概念

其他资源

转载于:https://www.cnblogs.com/xushining/p/3685364.html

你可能感兴趣的文章
NoSql数据库使用半年后在设计上面的一些心得 (转)
查看>>
乐在其中设计模式(C#) - 原型模式(Prototype Pattern)
查看>>
纯JavaScripst的全选、全不选、反选 【转】
查看>>
node-webkit学习(3)Native UI API概览
查看>>
基于Windows 配置 nginx 集群 & 反向代理
查看>>
jquery 新建的元素事件绑定问题研究[转]
查看>>
cacti 添加
查看>>
maven中Rhino classes (js.jar) not found - Javascript disabled的处理
查看>>
LTS学习
查看>>
对SIGQUIT的实验 & Java dump
查看>>
软件架构分类(转载)
查看>>
SQL 关于apply的两种形式cross apply 和 outer apply, with cube 、with rollup 和 grouping
查看>>
1028. List Sorting (25)
查看>>
NIO框架之MINA源码解析(四):粘包与断包处理及编码与解码
查看>>
LINUX负载均衡LVS-DR搭建
查看>>
根据xlsx模板生成excel数据文件发送邮件代码
查看>>
Python基础教程 - Tdcqma
查看>>
Gulp安装及使用
查看>>
Nginx教程(五) Nginx配置文件详解
查看>>
一篇文章,带你明确什么是过拟合,欠拟合以及交叉验证
查看>>