C#中的委托和匿名方法 [英] delegate and anonynous method in C#

查看:283
本文介绍了C#中的委托和匿名方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,您能给我解释一下,Linq中匿名方法和委托如何联系在一起.谢谢.

Hello all, Could you explain to me, how anonymous method and delegate relate together in Linq. Thank you.

推荐答案

您可以查找一下,这可能是最好的,因为这些机构都在告诉您答案.

委托定义分配给名称的签名,以便可以对其进行引用.有点像"int".这样,可以引用方法并将其作为参数传递或保存在变量中.它也与事件一起使用.自第一个版本发布以来,代表们就一直使用C#.在以后的发行版中,有许多委托被定义为语言的一部分,这些委托利用了泛型的优势,包括Action和Func.这意味着不必定义委托.

匿名方法实际上是非常不同的.它基本上是一种没有名称的方法,因此是匿名的.同样,匿名方法可能没有类型的返回类型和参数(您仍然可以键入它们,但大多数人没有),这是常规方法所必需的,并且匿名方法可以在定义它的方法中使用变量(并且它必须在方法中定义).匿名方法的问题在于它们难以重用(可能将匿名方法分配给委托定义的变量,但不确定).
You can look this up, and that is probably best since those are authorities that are telling you the answer.

A delegate defines a signature that is assigned to name so that it can be referenced. Sort of like "int". That way a method can be referenced and passed as an argument or saved in a variable. It is also used with events. Delegates have been in C# since the first release. There are a number of delegates that are defined as part of the language in the later releases that take advanatage of generics, including Action and Func. This means that do not have to define a delegate.

An anonymous method is actually very different. It is basically a method without a name, thus the term anonymous. Also an anonymous method may not have the return type and arguments typed (you can still type them, but most people do not), which is required for normal methods, and an anonymous method can use variable within the method that defines it (and it has to be defined in a method). The problem with anonymous methods is that they are hard to reuse (could possibly assign an anonymous method to a variable defined by a delegate, but not sure).



这篇关于C#中的委托和匿名方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆