有Java的标记符在C#或替代方法相当于一个 [英] is there a equivalent of Java's labelled break in C# or a workaround
问题描述
我把一些Java代码到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;
}
}
}
有没有在C#中的等价(当前的读数表明没有),如果不是有比(说)有布尔标志指示是否在每个循环结束时(例如)
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
我会有兴趣,为什么C#没有这个,因为它似乎不是很邪恶的。
I'd be interested as to why C# doesn't have this as it doesn't seem to be very evil.
推荐答案
可以只需使用 转到
一>,将直接跳转到一个标签。
You can just use goto
to jump directly to a label.
while (somethingA)
{
// ...
while (somethingB)
{
if (condition)
{
goto label1;
}
}
}
label1:
// ...
在类似C语言中,转到
往往最终打破嵌套循环清洁剂,而不是跟踪布尔变量,并反复检查他们在每个循环结束
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.
这篇关于有Java的标记符在C#或替代方法相当于一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!