是否有相当于 C# 中 Java 标记的中断或解决方法
我正在将一些 Java 代码转换为 C#,并发现了一些标记为“break”的语句(例如)
label1:
while (somethingA) {
...
while (somethingB) {
if (condition) {
break label1;
}
}
}
C# 中是否有等效的语句(当前阅读建议没有),如果没有,除了(例如)使用 bool 标志来指示之外,是否还有任何转换是否在每个循环结束时中断(例如)
bool label1 = false;
while (somethingA)
{
...
while (somethingB)
{
if (condition)
{
label1 = true;
break;
}
}
if (label1)
{
break;
}
}
// breaks to here
我很感兴趣为什么 C# 没有这个,因为它似乎不是很邪恶。
I am converting some Java code to C# and have found a few labelled "break" statements (e.g.)
label1:
while (somethingA) {
...
while (somethingB) {
if (condition) {
break label1;
}
}
}
Is there an equivalent in C# (current reading suggests not) and if not is there any conversion other than (say) having bool flags to indicate whether to break at each loop end (e.g.)
bool label1 = false;
while (somethingA)
{
...
while (somethingB)
{
if (condition)
{
label1 = true;
break;
}
}
if (label1)
{
break;
}
}
// breaks to here
I'd be interested as to why C# doesn't have this as it doesn't seem to be very evil.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需使用
goto
< /a> 直接跳转到标签。在类 C 语言中,
goto
通常会更干净地打破嵌套循环,而不是跟踪布尔变量并在每个循环结束时重复检查它们。You can just use
goto
to jump directly to a label.In C-like languages,
goto
often ends up cleaner for breaking nested loops, as opposed to keeping track of boolean variables and repeatedly checking them at the end of each loop.