什么是.NET Framework中的lambda表达式和代表之间的区别? [英] What is the difference between lambdas and delegates in the .NET Framework?

查看:223
本文介绍了什么是.NET Framework中的lambda表达式和代表之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被问这个问题了很多,我想我会征求对如何最好地形容差一些输入。


解决方案

他们实际上是两个完全不同的事情。 委派实际上是用于保存的方法或λ的引用的变量的名称,以及一个lambda是不具有永名称的方法<​​/ P>

lambda表达式非常。就像其他方法,除了几个细微的差别。




  1. 一个正常的方法是在一个的\"statement并绑一个永久的名字,而一个lambda定义对飞在的表情并没有永久名称。

  2. 某些lambda表达式可以使用.NET表达式目录树使用,而方法。不能



一个委托的定义是这样的:

 委托的Int32 BinaryIntOp(的Int32 X,的Int32 Y); 



型BinaryIntOp的变量可以有一个方法或者分配给它的labmda,只要签名是相同的:两个的Int32参数,一个Int32返回



一个拉姆达可能像这样定义的:

  BinaryIntOp sumOfSquares =(A,b)=> A * A + B * B; 



另外要注意的是,尽管通用的函数功能和动作类型通常被认为是拉姆达类型,他们就像任何其他代表。关于他们的好处是,他们基本上是任何类型的委托,你可能需要(最多4个参数,虽然你当然可以添加更多自己的)定义一个名称。所以,如果你使用的是各种各样的委托类型,但没有超过一次,你应该避免通过FUNC键,操作混乱与委托声明你的代码。



下面是的Func键和行动是如何不只是lambda表达式的说明:

 的Int32 DiffOfSquares(的Int32 X,的Int32 Y)
{
返回X * X - Y *Ÿ;
}

Func键<的Int32,的Int32,的Int32> funcPtr = DiffOfSquares;

要知道另一个有用的事情是,委托类型具有相同签名但不同的名称(不是方法本身)将不被隐式地浇铸到彼此。这包括Func键和行动的代表。但是,如果签名是相同的,你可以明确地投在它们之间。



要加倍努力....在C#中的功能是灵活的,随用随lambda表达式和代表。但C#不具有一流的功能。您可以使用分配给委托变量函数的名称基本上创建代表该函数的对象。但它真的是一个编译器的把戏。如果你写的函数名后加一个点开始的声明(即尝试做对函数本身成员访问),你会发现有没有成员那里借鉴。即使不从对象的人。这可以防止执行有用的(和潜在的危险,当然)的东西如添加扩展方法可以在任何函数调用的程序员。你能做的最好的是延长Delegate类本身,这是肯定也是有用的,但没有这么多



更​​新:又见的Karg's回答说明VS匿名委托的区别。方法与放大器; 。lambda表达式



更​​新2:的詹姆斯·哈特提出了一个重要的,虽然很技术,注意lambda表达式和代表不是.NET实体(即CLR没有委托或lambda的概念),而是它们的框架和语言构造<​​/ p>

I get asked this question a lot and I thought I'd solicit some input on how to best describe the difference.

解决方案

They are actually two very different things. "Delegate" is actually the name for a variable that holds a reference to a method or a lambda, and a lambda is a method without a permanent name.

Lambdas are very much like other methods, except for a couple subtle differences.

  1. A normal method is defined in a "statement" and tied to a permanent name, whereas a lambda is defined "on the fly" in an "expression" and has no permanent name.
  2. Some lambdas can be used with .NET expression trees, whereas methods cannot.

A delegate is defined like this:

delegate Int32 BinaryIntOp(Int32 x, Int32 y);

A variable of type BinaryIntOp can have either a method or a labmda assigned to it, as long as the signature is the same: two Int32 arguments, and an Int32 return.

A lambda might be defined like this:

BinaryIntOp sumOfSquares = (a, b) => a*a + b*b;

Another thing to note is that although the generic Func and Action types are often considered "lambda types", they are just like any other delegates. The nice thing about them is that they essentially define a name for any type of delegate you might need (up to 4 parameters, though you can certainly add more of your own). So if you are using a wide variety of delegate types, but none more than once, you can avoid cluttering your code with delegate declarations by using Func and Action.

Here is an illustration of how Func and Action are "not just for lambdas":

Int32 DiffOfSquares(Int32 x, Int32 y)
{
  return x*x - y*y;
}

Func<Int32, Int32, Int32> funcPtr = DiffOfSquares;

Another useful thing to know is that delegate types (not methods themselves) with the same signature but different names will not be implicitly casted to each other. This includes the Func and Action delegates. However if the signature is identical, you can explicitly cast between them.

Going the extra mile.... In C# functions are flexible, with the use of lambdas and delegates. But C# does not have "first-class functions". You can use a function's name assigned to a delegate variable to essentially create an object representing that function. But it's really a compiler trick. If you start a statement by writing the function name followed by a dot (i.e. try to do member access on the function itself) you'll find there are no members there to reference. Not even the ones from Object. This prevents the programmer from doing useful (and potentially dangerous of course) things such as adding extension methods that can be called on any function. The best you can do is extend the Delegate class itself, which is surely also useful, but not quite as much.

Update: Also see Karg's answer illustrating the difference between anonymous delegates vs. methods & lambdas.

Update 2: James Hart makes an important, though very technical, note that lambdas and delegates are not .NET entities (i.e. the CLR has no concept of a delegate or lambda), but rather they are framework and language constructs.

这篇关于什么是.NET Framework中的lambda表达式和代表之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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