为什么switch语句中的yield return后面需要break?
有人可以告诉我为什么编译器认为在以下代码中的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为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).
这个问题假设了一个不正确的前提。 switch 语句中的yield 返回之后不需要有break 语句。例如:
这里我们在 switch 语句中有一个yield return,后面没有一个break。接下来是对 M() 的调用,然后是 throw 语句。这是完全合法的。
真正的规则是开关部分的终点一定不能到达。
Break、Continue、Return、Goto、Return 和 throw 的终点是不可到达的,因为所有这些语句都分支到另一个位置并且不会返回,因此紧随其后的代码标记是不可到达的。这与方法调用(分支到另一个位置然后返回)或yield return(将控制权转移回调用者)形成鲜明对比;当调用者将控制权转移回迭代器块时,控制权将从yield return停止的地方开始,因此yield return的终点是可以到达的。
如果您对这个主题感兴趣,我建议您阅读规范的第 8.1 节。
The question supposes an incorrect premise. A break statement is not required after a yield return in a switch statement. For example:
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.
“正常”返回有两个功能:
ýield return 仅标记返回值;它不影响控制流。在yield return 语句之后,程序将在下一行继续执行,在示例中需要中断,就像在任何其他 switch-case 语句中一样。
A "normal" return has two functions:
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.