C# 操作 lambda 限制

发布于 2024-07-07 11:28:29 字数 438 浏览 3 评论 0原文

为什么这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小清晰的声音 2024-07-14 11:28:29

唔。 我已经有了答案,但答案并不好。

我不相信有“抛出”表达式。 有一个 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?

口干舌燥 2024-07-14 11:28:29

这是我的看法:

throw 是一个语句,而不是一个表达式。

以及参考:

12.3.3.11 抛出语句

对于表单的语句 stmt

抛出表达式;

v 的明确赋值状态
expr 的开头是相同的
作为 v 的确定赋值状态
在 stmt 的开头。

为了解释其本质,也许我们应该考虑一下表达式在 C# lambda 构造中意味着什么。 它只是语法糖:

delegate () { return XXX; }

其中 XXX 是一个表达式

Here's my take:

throw is a statement, not an expression.

And the reference:

12.3.3.11 Throw statements

For a statement stmt of the form

throw expr;

the definite assignment state of v
at the beginning of expr is the same
as the definite assignment state of v
at the beginning of stmt.

To explain the essence perhaps one should think about what an expression implies within the C# lambda construct. It is simply syntactic sugar for:

delegate () { return XXX; }

where XXX is an expression

小糖芽 2024-07-14 11:28:29

您无法从无作用域的 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 returning void. Why the compiler team didn't handle this situation, I don't know.

春庭雪 2024-07-14 11:28:29

我可以从这里找到所有参考资料:

http ://msdn.microsoft.com/en-us/library/ms364047(VS.80).aspx#cs3spec_topic4

显示您有两个选项:

Action a = () => { throw new InvalidOperationException(); };

Action a = () => throw new InvalidOperationException()

注意缺少的 ; 在最后。 是的,这对我来说也毫无意义。 他们在规范中给出的示例是:

x => x + 1                     // Implicitly typed, expression body
x => { return x + 1; }         // Implicitly typed, statement body
(int x) => x + 1               // Explicitly typed, expression body
(int x) => { return x + 1; }   // Explicitly typed, statement body
(x, y) => x * y               // Multiple parameters
() => Console.WriteLine()      // No parameters

不知道这有多大帮助 - 我无法判断您在什么上下文中使用它,并且不添加 ; 最后在 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:

Action a = () => { throw new InvalidOperationException(); };

or

Action a = () => throw new InvalidOperationException()

Note the missing ; on the end. Yes, it makes no sense to me either. The examples they give in the spec are:

x => x + 1                     // Implicitly typed, expression body
x => { return x + 1; }         // Implicitly typed, statement body
(int x) => x + 1               // Explicitly typed, expression body
(int x) => { return x + 1; }   // Explicitly typed, statement body
(x, y) => x * y               // Multiple parameters
() => Console.WriteLine()      // No parameters

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!

芯好空 2024-07-14 11:28:29

没什么大惊喜。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文