为什么不能将匿名方法分配给 var? [英] Why can't an anonymous method be assigned to var?

查看:27
本文介绍了为什么不能将匿名方法分配给 var?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

Func<string, bool> comparer = delegate(string value) {
    return value != "0";
};

但是,以下内容无法编译:

However, the following does not compile:

var comparer = delegate(string value) {
    return value != "0";
};

为什么编译器不能确定它是一个Func?它接受一个字符串参数,并返回一个布尔值.相反,它给了我错误:

Why can't the compiler figure out it is a Func<string, bool>? It takes one string parameter, and returns a boolean. Instead, it gives me the error:

无法将匿名方法分配给隐式类型的局部变量.

Cannot assign anonymous method to an implicitly-typed local variable.

我有一个猜测,那就是如果编译了 var 版本,如果我有以下内容,它将缺乏一致性:

I have one guess and that is if the var version compiled, it would lack consistency if I had the following:

var comparer = delegate(string arg1, string arg2, string arg3, string arg4, string arg5) {
    return false;
};

以上没有意义,因为 Func<> 只允许最多 4 个参数(在 .NET 3.5 中,这是我正在使用的).也许有人可以澄清这个问题.谢谢.

The above wouldn't make sense since Func<> allows only up to 4 arguments (in .NET 3.5, which is what I am using). Perhaps someone could clarify the problem. Thanks.

推荐答案

其他人已经指出,您可以表示有无数种可能的委托类型;Func 有什么特别之处以至于它应该成为默认值而不是 PredicateAction 或任何其他可能性?并且,对于 lambda,为什么明显的意图是选择委托形式,而不是表达式树形式?

Others have already pointed out that there are infinitely many possible delegate types that you could have meant; what is so special about Func that it deserves to be the default instead of Predicate or Action or any other possibility? And, for lambdas, why is it obvious that the intention is to choose the delegate form, rather than the expression tree form?

但是我们可以说 Func 是特殊的,并且 lambda 或匿名方法的推断类型是某物的 Func.我们还是会遇到各种各样的问题.对于以下情况,您希望推断出哪些类型?

But we could say that Func is special, and that the inferred type of a lambda or anonymous method is Func of something. We'd still have all kinds of problems. What types would you like to be inferred for the following cases?

var x1 = (ref int y)=>123;

没有 Func 类型可以接受任何引用.

There is no Func<T> type that takes a ref anything.

var x2 = y=>123;

我们不知道形参的类型,但我们知道返回值.(还是我们?返回的是 int?long?short?byte?)

We don't know the type of the formal parameter, though we do know the return. (Or do we? Is the return int? long? short? byte?)

var x3 = (int y)=>null;

我们不知道返回类型,但它不能为空.返回类型可以是任何引用类型或任何可空值类型.

We don't know the return type, but it can't be void. The return type could be any reference type or any nullable value type.

var x4 = (int y)=>{ throw new Exception(); }

同样,我们不知道返回类型,这一次它可以为空.

Again, we don't know the return type, and this time it can be void.

var x5 = (int y)=> q += y;

这是一个返回 void 的语句 lambda 还是返回分配给 q 的值的东西?两者都是合法的;我们应该选择哪个?

Is that intended to be a void-returning statement lambda or something that returns the value that was assigned to q? Both are legal; which should we choose?

现在,您可能会说,好吧,只是不支持这些功能中的任何一个.只支持可以计算类型的正常"情况.那没有帮助.这如何让我的生活更轻松?如果该功能有时有效,有时失败,那么我仍然必须编写代码来检测所有这些失败情况,并为每种情况提供有意义的错误消息.我们仍然必须指定所有这些行为,记录它,为它编写测试,等等.这是一项非常昂贵的功能,可为用户节省大约六次击键次数.我们有更好的方法来增加语言的价值,而不是花费大量时间为一个功能编写测试用例,而这个功能有一半时间不起作用,并且在它起作用的情况下几乎没有提供任何好处.

Now, you might say, well, just don't support any of those features. Just support "normal" cases where the types can be worked out. That doesn't help. How does that make my life easier? If the feature works sometimes and fails sometimes then I still have to write the code to detect all of those failure situations and give a meaningful error message for each. We still have to specify all that behaviour, document it, write tests for it, and so on. This is a very expensive feature that saves the user maybe half a dozen keystrokes. We have better ways to add value to the language than spending a lot of time writing test cases for a feature that doesn't work half the time and doesn't provide hardly any benefit in cases where it does work.

它真正有用的情况是:

var xAnon = (int y)=>new { Y = y };

因为那个东西没有可说"的类型.但是我们一直有这个问题,我们只是用方法类型推断来推导类型:

because there is no "speakable" type for that thing. But we have this problem all the time, and we just use method type inference to deduce the type:

Func<A, R> WorkItOut<A, R>(Func<A, R> f) { return f; }
...
var xAnon = WorkItOut((int y)=>new { Y = y });

现在方法类型推断计算出 func 类型是什么.

and now method type inference works out what the func type is.

这篇关于为什么不能将匿名方法分配给 var?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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