gpt4 book ai didi

c# - 如何跳出 IF 语句

转载 作者:IT王子 更新时间:2023-10-29 04:26:20 26 4
gpt4 key购买 nike

我有这样的代码:

public void Method()
{
if(something)
{
// Some code
if(something2)
{
// Now I should break from ifs and go to the code outside ifs
}
return;
}
// The code I want to go if the second if is true
}

是否有可能在 if 之后转到该代码而不使用任何 go to 语句或将其余代码提取到其他方法?


是的,我知道 Else ;)
但是这段代码太长了,应该在第一个 IF 为假且第一个 IF 为真时运行,第二个 IF 为假。所以提取一个我认为最好的方法。

最佳答案

回答你的问题:

public void Method()
{
do
{
if (something)
{
// some code
if (something2)
{
break;
}

return;
}
break;
}
while( false );

// The code I want to go if the second `if` is true
}

关于c# - 如何跳出 IF 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29297305/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com