<分区>
如果我有一个 lambda,例如 () => { throw new Exception(); }
,不清楚它是否有返回类型。因此,它可以(隐式)转换为 Action
。和 Func<object>
(或任何其他 Func<T>
)。这是因为,根据 C# 4 规范的 §6.5 匿名函数转换:
[A] delegate type
D
is compatible with an anonymous functionF
provided:
…
If
D
has avoid
return type and the body ofF
is a statement block, when […] the body ofF
is a valid statement block in which no return statement specifies an expression.If
D
has a non-void return type and the body ofF
is a statement block, when […] the body ofF
is a valid statement block with a non-reachable end point in which eachreturn
statement specifies an expression that is implicitly convertible to the return type ofD
.
但是如果我有一个方法的两个重载,其中一个有一个类型为 Action
的参数和另一个Func<object>
,然后我从上方将 lambda 传递给它,Func<object>
使用过载。为什么?规范的哪一部分说 Func<object>
优于Action
在这种情况下?
我看过 §7.5.3.2 更好的函数成员,但这并不能解释它。