当我尝试在 C# 中创建代码标签(用于转到)时,为什么会出现语法错误?
Why am I getting a syntax error when I try to create a code label in C# (for goto)?
考虑以下因素:
class abc {
public void foobar() {
while(true) {
while(true) {
goto mylabel;
}
}
mylabel:
}
}
我在 mylabel 的 : 上收到一个语法错误,上面写着:
Only assignment, call, increment, decrement, await, and new object
expressions can be used as a statement
我是不是疯了?我到底做错了什么?我用谷歌搜索了一下,看来我的语法是正确的。
在标签之后,你需要一个声明。关闭 }
不是语句,它是结束一段代码。您可以在此处使用 empty statement (;
):
mylabel: ;
链接页面甚至指出:
Also, an empty statement can be used to declare a label just before the closing "}" of a block:
考虑以下因素:
class abc {
public void foobar() {
while(true) {
while(true) {
goto mylabel;
}
}
mylabel:
}
}
我在 mylabel 的 : 上收到一个语法错误,上面写着:
Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
我是不是疯了?我到底做错了什么?我用谷歌搜索了一下,看来我的语法是正确的。
在标签之后,你需要一个声明。关闭 }
不是语句,它是结束一段代码。您可以在此处使用 empty statement (;
):
mylabel: ;
链接页面甚至指出:
Also, an empty statement can be used to declare a label just before the closing "}" of a block: