C# 中的 goto 语句
我正在编写一个类似于 C# 的函数:
public void CountNumber()
{
for(int i = 0; i < 40; i++) {
if(i > 20) {
goto out1;
}
Console.WriteLine("hello " + 1);
out1:
string hello = "";
}
}
这基本上会计算数字,如果 i 大于 20,则不应写入 console.writeline。它应该跳过并点击“out1”,但“out1”最终需要有一个函数才能编译。它需要有“string hello =”“”才能编译。我不需要“字符串 hello =”“”。我只是想让它什么都不做并结束循环。有没有办法在没有 out1: 语句需要的“string hello =”“”的情况下做到这一点?喜欢:
public void CountNumber()
{
for(int i = 0; i < 40; i++) {
if(i > 20) {
goto out1;
}
Console.WriteLine("hello " + 1);
out1:
}
}
谢谢。
I'm writing a function like in C#:
public void CountNumber()
{
for(int i = 0; i < 40; i++) {
if(i > 20) {
goto out1;
}
Console.WriteLine("hello " + 1);
out1:
string hello = "";
}
}
This basically counts the number and if the i is greater than 20 it should not write to console.writeline. it should step over and hit "out1" but the "out1" needs to have a function in the end to compile. It needs to have "string hello = """ to compile. I don't need the "string hello = """. I just want it to do nothing and got the end of the loop. Is there a way to do this without the "string hello = """ that the out1: statement needs? Like:
public void CountNumber()
{
for(int i = 0; i < 40; i++) {
if(i > 20) {
goto out1;
}
Console.WriteLine("hello " + 1);
out1:
}
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
虽然说有比使用 goto 更好的方法来解决这个问题是绝对正确的,但我注意到没有人真正回答你的问题。
标签必须标记一个语句。您想去一个没有与之关联的语句的位置。您可以使用单个分号或空块来创建空语句。
或者
但就像他们说的,如果可以避免的话,首先不要去那里。
Though it is absolutely correct to say that there are better ways to solve this problem than to use goto, I note that no one has actually answered your question.
A label must label a statement. You want to go to a location that has no statement associated with it. You can either make an empty statement with a single semicolon, or an empty block.
or
But like they say, don't go there in the first place if you can avoid it.
这个循环可以很容易地用许多其他方式编写 - 您可以只循环 while
i<=20
而不是i<40
(最好),或者移动Console。 WriteLine
调用 if 语句,并反转 if。但是,我假设您正在尝试在“真实”案例中处理更复杂的场景。如果是这样的话,不用使用
goto
,而是使用continue
来跳过循环的其余部分:同样,您可以使用
break
来完全跳过 循环。跳出循环并且不处理更多元素(如果这更适合您的实际情况)。This loop could easily be written many other ways - you could just loop while
i<=20
instead ofi<40
(best), or move theConsole.WriteLine
call into the if statement with the if inverted.However, I'm assuming you're trying to work with a more elaborate scenario in your "real" case. If that's the case, instead of using
goto
, just usecontinue
to skip the rest of the loop:Similarly, you can use
break
to completely break out of the loop and not process more elements, if that's more appropriate in your real case.只需反转你的条件 -
if...else
也可能是一种替代方案。我假设还有其他代码,否则您只需更改for
循环本身即可计数到 20。Just invert your condition - also
if...else
might be an alternative. I assume there is other code otherwise you can just change thefor
loop itself to just count up to 20.还有一些其他类似 goto 的语句,您应该考虑使用:
continue
转到当前循环的下一次迭代。break
离开当前循环return
退出当前方法如果以上都没有达到您想要的效果,您应该只考虑
goto
。根据我的经验,这种情况很少见。看起来您想在此处使用
继续
。There are some other goto-like statements, you should consider using:
continue
goes to the next iteration of the current loop.break
leaves the current loopreturn
exits the current methodYou should only consider
goto
if none of the above does what you want. And in my experience that's very rarely the case.It looks like you want to use
continue
here.您可以使用
continue
关键字来实现:但是,请考虑使用
if
来代替:You can use the
continue
keyword for that:However, consider using the
if
instead: