new Action() 和 lambda 有什么区别?

编程入门 行业动态 更新时间:2024-10-27 02:21:23
本文介绍了new Action() 和 lambda 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以当我写这样的东西时

So when I write something like this

Action action = new Action(()=>_myMessage = "hello");

重构专业版!突出显示这是一个冗余的委托创建,并允许我将其缩短为

Refactor Pro! Highlights this as a redundant delegate creation and allows me to to shorten it to

Action action = () => _myMessage="hello";

这通常效果很好.通常,但并非总是如此.例如,Rhino Mocks 有一个名为 Do 的扩展方法:

And this usually works great. Usually, but not always. For example, Rhino Mocks has an extension method named Do:

IMethodOptions<T> Do(Delegate action);

在这里,传入第一个版本有效,但第二个无效.这里到底发生了什么?

Here, passing in the first version works, but the second doesn't. What exactly is going on under the covers here?

推荐答案

第一个版本有效地做了:

The first version is effectively doing:

Action tmp = () => _myMessage = "hello"; var action = new Action(tmp);

您遇到的问题是编译器必须知道 lambda 表达式应该转换成什么样的委托(或表达式树).这就是为什么:

The problem you're running into is that the compiler has to know what kind of delegate (or expression tree) the lambda expression should be converted into. That's why this:

var action = () => _myMessage="hello";

实际上无法编译 - 它可以是 any 没有参数的委托类型,也可以是没有返回值或与 _myMessage 相同的返回类型(大概是 string).例如,所有这些都是有效的:

actually doesn't compile - it could be any delegate type with no parameters and either no return value or the same return type as _myMessage (which is presumably string). For instance, all of these are valid:

Action action = () => _myMessage="hello"; Func<string> action = () => _myMessage="hello"; MethodInvoker action = () => _myMessage="hello"; Expression<Action> = () => _myMessage="hello"; // etc

如果使用 var 声明,C# 编译器如何计算 action 的含义?

How could the C# compiler work out what type action was meant to be, if it were declared with var?

在调用方法(对于您的 Rhino Mocks 示例)时解决此问题的最简单方法是强制转换:

The simplest way to get round this when calling a method (for your Rhino Mocks example) is to cast:

methodOptions.Do((Action) (() => _myMessage = "hello"));

更多推荐

new Action() 和 lambda 有什么区别?

本文发布于:2023-11-07 04:12:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565546.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   Action   lambda

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!