为什么switch语句中的yield return后面需要break?

发布于 2024-08-23 22:43:01 字数 667 浏览 8 评论 0原文

有人可以告诉我为什么编译器认为在以下代码中的 yield return 之后需要 break 吗?

foreach (DesignerNode node in nodeProvider.GetNodes(span, node => node.NodeType != NDjango.Interfaces.NodeType.ParsingContext))
{
    switch (node.ErrorMessage.Severity)
    {
        case -1:
        case 0:
            continue;
        case 1:
            yield return new TagSpan<ErrorTag>(node.SnapshotSpan, new ErrorTag(PredefinedErrorTypeNames.Warning));
            break;
        default:
            yield return new TagSpan<ErrorTag>(node.SnapshotSpan, new ErrorTag(PredefinedErrorTypeNames.SyntaxError));
            break;
    }
}

Can somebody tell me why compiler thinks that break is necessary after yield return in the following code?

foreach (DesignerNode node in nodeProvider.GetNodes(span, node => node.NodeType != NDjango.Interfaces.NodeType.ParsingContext))
{
    switch (node.ErrorMessage.Severity)
    {
        case -1:
        case 0:
            continue;
        case 1:
            yield return new TagSpan<ErrorTag>(node.SnapshotSpan, new ErrorTag(PredefinedErrorTypeNames.Warning));
            break;
        default:
            yield return new TagSpan<ErrorTag>(node.SnapshotSpan, new ErrorTag(PredefinedErrorTypeNames.SyntaxError));
            break;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

謸气贵蔟 2024-08-30 22:43:01

因为yield return 只是迭代器生成器的语法糖,并且您实际上并没有退出任何方法。并且 C# 不允许 switch 语句(而且看起来您并不希望在这里使用它)。

Because yield return is just syntactic sugar for an iterator generator, and you're not actually exiting any method. And C# doesn't allow fall-through in switch statements (and it doesn't look like you want it here anyway).

芯好空 2024-08-30 22:43:01

为什么 switch 语句中 return Yield 后需要break?

这个问题假设了一个不正确的前提。 switch 语句中的yield 返回之后不需要有break 语句。例如:

switch(x)
{
case 123:
    yield return 456;
    M();
    throw new Exception();
case 789: // and so on

这里我们在 switch 语句中有一个yield return,后面没有一个break。接下来是对 M() 的调用,然后是 throw 语句。这是完全合法的。

真正的规则是开关部分的终点一定不能到达

Break、Continue、Return、Goto、Return 和 throw 的终点是不可到达的,因为所有这些语句都分支到另一个位置并且不会返回,因此紧随其后的代码标记是不可到达的。这与方法调用(分支到另一个位置然后返回)或yield return(将控制权转移回调用者)形成鲜明对比;当调用者将控制权转移回迭代器块时,控制权将从yield return停止的地方开始,因此yield return的终点是可以到达的。

如果您对这个主题感兴趣,我建议您阅读规范的第 8.1 节。

Why break is required after return yield in a switch statement?

The question supposes an incorrect premise. A break statement is not required after a yield return in a switch statement. For example:

switch(x)
{
case 123:
    yield return 456;
    M();
    throw new Exception();
case 789: // and so on

Here we have a yield return in a switch statement which is not followed by a break. It is followed by a call to M(), and then a throw statement. That's perfectly legal.

The real rule is that the end point of a switch section must not be reachable.

The end points of a break, continue, return, goto, return and throw are not reachable because all of those statements branch to another location and do not come back, so that the code token immediately following them is not reachable. This is in contrast with, say, a method call, which branches to another location and then comes back, or a yield return, which transfers control back to the caller; when the caller transfers control back to the iterator block, control will pick up where the yield return left off, so therefore the end point of the yield return is reachable.

If this subject interests you, I recommend reading section 8.1 of the specification.

拍不死你 2024-08-30 22:43:01

“正常”返回有两个功能:

  1. 标记函数的返回值。
  2. 将控制权转移给调用者。

ýield return 仅标记返回值;它不影响控制流。在yield return 语句之后,程序将在下一行继续执行,在示例中需要中断,就像在任何其他 switch-case 语句中一样。

A "normal" return has two functions:

  1. Mark the return value of the function.
  2. Transfer control to the caller.

A ýield return only marks a return value; it does not affect the control flow. After a yield return statement the program execution continues on the next line, where a break is needed in your sample, just as in any other switch-case statement.

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