运算符'=>' 的作用是什么? 在 C# 中是什么意思?
'=>' 是什么意思 在此声明中表示?
del = new SomeDelegate(() => SomeAction());
上面的声明和这个一样吗?
del = new SomeDelegate(this.SomeAction);
谢谢。
What does the '=>' in this statement signify?
del = new SomeDelegate(() => SomeAction());
Is the above declaration the same as this one?
del = new SomeDelegate(this.SomeAction);
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
基本上它指定一个匿名函数,不带任何参数来调用 SomeAction。 所以是的,它们在功能上是等效的。 虽不相等。 使用 lambda 更相当于:
其中 CallSomeAction 定义为:
希望有帮助!
Basically it's specifying an anonymous function, that takes no parameters that calls SomeAction. So yes, they are functionally equivalent. Though not equal. Using the lambda is more equivalent to:
where CallSomeAction is defined as:
Hope that helps!
“=>” 可以读作“转到”(来源:Eric Lippert),并简单地将参数与 lambda 表达式中的操作分开。 在这种情况下,lambda 就显得有些过分了。 更好的例子是:(
查找项目类型为 Foo 的所有项目)
等。在 C# 2.0 中,也可以这样写:
And 是表达函数“内联”的快速方式,同时还提供“闭包”支持 - 即也可能是:
否则,需要类型定义来传入项目类型:
事实上,这几乎正是编译器为我们所做的事情(对于“委托”和 lambda 用法)。 最大的区别是 lambda还可以表达
表达式
,例如 LINQ。The "=>" can be read "goes to" (source: Eric Lippert), and simply separates the argument(s) from the operation in a lambda expression. In this case, a lambda is overkill. Better examples would be:
(find all items where the item's type is Foo)
etc. In C# 2.0, this can also be written:
And is a quick way of expression a function "inline", while also offering "closure" support - i.e. it could also be:
To do this otherwise would require a type-definiton to pass in the item-type:
In fact, this is pretty-much exactly what the compiler does for us (both for "delegate" and lambda usage). The biggest difference is that a lambda can also express an
Expression
, for example for LINQ.它们做相同的事情,但“() => ...”语法就是所谓的 lambda 表达式,因此与匿名函数相同。 您可能可以完全省略委托部分,只让编译器为您推断委托类型。
取决于“del”被声明为什么类型。
编辑
使用 lambda 或匿名方法或仅使用常规方法(对于初学者来说)使您能够将没有委托签名的方法映射到委托。
例如,假设您有一个签名为 bool myDelegate(int, int) 的委托,但您希望有一个签名为 bool myMethod(string, string) 的方法来处理该委托。 然后,您可以使用 lambda 表达式,通过像这样的简短语法内联执行此操作。
正如您所看到的,lambda 和匿名方法基本上只是一种更短/内联的方式来创建处理委托的方法。 在您的情况下,您可能不需要创建额外的方法。 这仅取决于委托签名是否与您的方法签名相同,对我来说,似乎是这样。
They do the same thing but the "() => ..." syntax is what is called a lambda expression and as such is the same as an anonymous function. You could probably leave out the delegate part altogether and just let the compiler infer the delegate type for you.
Depending on what type "del" is seclared as.
Edit
Using lambdas or anonymous methods or just a regular method for starters enables you to map methods that didn't have the delegate's signature to the delegate.
For example, say you have a delegate with the signature bool myDelegate(int, int) but you wanted to have a method with the signature bool myMethod(string, string) handle the delegate. You could then use the lambda expression to let you do this inline with a short syntax like so.
So as you can see lambdas and anonymous methods are basically just a shorter/inline way of making a method to handle the delegate. In you case you might not need to make an extra method. It just depends on if the delegate signature is the same as your method signature, and to me, it seems like it is.
=>
是 Lambda 运算符 , lambda 表达式就像 C# 2.0 匿名方法的演变。您可以以非常相似的方式使用匿名方法和 lambda 表达式来创建委托实例:
使用 lambda 表达式:
=>
is the Lambda Operator, lambda expressions are like an evolution of the C# 2.0 anonymous methods.You can use anonymous methods and lambda expressions in a very similar way to create delegate instances:
Using lambda expressions: