C# 操作 lambda 限制
为什么这个 lambda 表达式无法编译?
Action a = () => throw new InvalidOperationException();
推测很好,但我真的很感激 C# 语言规范或其他文档的参考。
是的,我知道以下内容是有效的并且可以编译:
Action a = () => { throw new InvalidOperationException(); };
这篇博文。
Why does this lambda expression not compile?
Action a = () => throw new InvalidOperationException();
Conjecture is fine, but I would really appreciate references to the C# language specification or other documentation.
And yes, I know that the following is valid and will compile:
Action a = () => { throw new InvalidOperationException(); };
The context where I would use something like this is described on this blog post.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
唔。 我已经有了答案,但答案并不好。
我不相信有“抛出”表达式。 有一个 throw 语句,但不仅仅是一个表达式。 将此与“Console.WriteLine()”进行比较,“Console.WriteLine()”是一个 void 类型的方法调用表达式。
同样,您不能将 switch 语句或 if 语句等单独作为 lambda 的主体。 你只能有一个表达式或一个块(第 7.14 节)。
这有什么帮助吗?
Hmm. I've got an answer, but it's not great.
I don't believe that there's a "throw" expression. There's a throw statement, but not just an expression. Compare this with "Console.WriteLine()" which is a method invocation expression with a void type.
As a parallel, you can't have a switch statement, or an if statement etc as the body of a lambda on its own. You can only have an expression or a block (section 7.14).
Is that any help?
这是我的看法:
throw
是一个语句,而不是一个表达式。以及参考:
为了解释其本质,也许我们应该考虑一下表达式在 C# lambda 构造中意味着什么。 它只是语法糖:
其中
XXX
是一个表达式Here's my take:
throw
is a statement, not an expression.And the reference:
To explain the essence perhaps one should think about what an expression implies within the C# lambda construct. It is simply syntactic sugar for:
where
XXX
is an expression您无法从无作用域的 lambda 中返回或抛出。
这样想...如果您不提供 {},编译器将确定您的隐式返回值是什么。 当您从 lambda 中
抛出
时,没有返回值。 你甚至没有返回void
。 我不知道为什么编译器团队不处理这种情况。You can't return or throw from an un-scoped lambda.
Think of it this way... If you don't provide a {}, the compiler determines what your implicit return value is. When you
throw
from within the lambda, there is no return value. You're not even returningvoid
. Why the compiler team didn't handle this situation, I don't know.我可以从这里找到所有参考资料:
http ://msdn.microsoft.com/en-us/library/ms364047(VS.80).aspx#cs3spec_topic4
显示您有两个选项:
或
注意缺少的 ; 在最后。 是的,这对我来说也毫无意义。 他们在规范中给出的示例是:
不知道这有多大帮助 - 我无法判断您在什么上下文中使用它,并且不添加 ; 最后在 C# 中没有任何意义,
区别可能在于它是一个表达式体 - 而不是一个语句 - 如果它没有 {}。 这意味着你的 throw 在那里无效,因为它是一个语句,而不是一个表达式!
All the references I can find, from here:
http://msdn.microsoft.com/en-us/library/ms364047(VS.80).aspx#cs3spec_topic4
show that you have two options:
or
Note the missing ; on the end. Yes, it makes no sense to me either. The examples they give in the spec are:
Dunno how much help that is - I can't tell what context you are using it in, and not putting a ; on the end makes no sense in C#
the difference may be that it's an expression body - not a statement - if it doesn't have the {}. Which means that your throw is not valid there, as it's a statement, not an expression!
没什么大惊喜。 Lambda 表达式是函数式编程的一个方面。 异常是过程式编程的一个方面。 两种编程风格之间的 C# 网格并不完美。
Not a big surprise. Lambda expressions are an aspect of functional programming. Exceptions are an aspect of procedural programming. The C# mesh between the two styles of programming isn't perfect.