Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何退出“大括号范围”?_C#_Break - Fatal编程技术网

C# 如何退出“大括号范围”?

C# 如何退出“大括号范围”?,c#,break,C#,Break,在C中是否可以退出一个作用域,比如说可以中断一个循环 private void a() { // do stuff here { // do more stuff here break;? //<-- jump out of this scope here!! break won't work // this further code should not be executed } // do stu

在C中是否可以退出一个作用域,比如说可以中断一个循环

private void a()
{
    // do stuff here

    {
        // do more stuff here

        break;? //<-- jump out of this scope here!! break won't work

        // this further code should not be executed
    }

    // do stuff here
}
您可以使用break来中断循环或开关,但不能中断这样的简单块

有很多方法可以实现这一点,例如使用goto或artificial while循环,但这听起来确实像是一种代码味道

你可以使用简单的条件来实现你想要的,这将使你的意图更加清晰

而不是:

DoSomething();
if (a == 1) // conditional break
{
    break;
}
DoSomethingElse();
break; // unconditional break (why though)
UnreachableCode(); // will generate compiler warning, by the way
你可以做:

DoSomething();
if (a != 1) // simple condition
{
    DoSomethingElse();
    if (false) // why though
    {
        UnreachableCode(); // will generate compiler warning, by the way
    }
}
或者,您可以将该部分提取到单独的命名方法,并使用返回语句进行短路。有时,它确实使代码更具可读性,特别是当您有返回值时:

private void a()
{
    // do stuff here

    MeaningfulNameToDescribeWhatYouDo();

    // do stuff here
}

private void MeaningfulNameToDescribeWhatYouDo()
{
    // do more stuff here

    if (condition)
    {
        return; //<-- jump out of this scope here!!
    }

    // this further code should not be executed
}     
您可以使用break来中断循环或开关,但不能中断这样的简单块

有很多方法可以实现这一点,例如使用goto或artificial while循环,但这听起来确实像是一种代码味道

你可以使用简单的条件来实现你想要的,这将使你的意图更加清晰

而不是:

DoSomething();
if (a == 1) // conditional break
{
    break;
}
DoSomethingElse();
break; // unconditional break (why though)
UnreachableCode(); // will generate compiler warning, by the way
你可以做:

DoSomething();
if (a != 1) // simple condition
{
    DoSomethingElse();
    if (false) // why though
    {
        UnreachableCode(); // will generate compiler warning, by the way
    }
}
或者,您可以将该部分提取到单独的命名方法,并使用返回语句进行短路。有时,它确实使代码更具可读性,特别是当您有返回值时:

private void a()
{
    // do stuff here

    MeaningfulNameToDescribeWhatYouDo();

    // do stuff here
}

private void MeaningfulNameToDescribeWhatYouDo()
{
    // do more stuff here

    if (condition)
    {
        return; //<-- jump out of this scope here!!
    }

    // this further code should not be executed
}     

是的,可以使用goto语句,但我强烈建议您在获得更多语言经验之前不要使用它们。我从来没有使用过goto,我也不知道有哪位程序员会这样做,因为它会把你的代码弄得一团糟,而且通常还有更好的选择

有一些方法可以负责任地使用它们,但从你的问题来看,你似乎不确定如何正确使用if/else/while等语句。相反,最好使用适当的流量控制


是的,可以使用goto语句,但我强烈建议您在获得更多语言经验之前不要使用它们。我从来没有使用过goto,我也不知道有哪位程序员会这样做,因为它会把你的代码弄得一团糟,而且通常还有更好的选择

有一些方法可以负责任地使用它们,但从你的问题来看,你似乎不确定如何正确使用if/else/while等语句。相反,最好使用适当的流量控制


把do放在{前面,whilefalse放在}后面,break将起作用…使用return;关键字您可以使用“中断”来中断循环或切换。@Alberto,这将永远不允许执行此处最后的//执行内容。@Fildor这只是代码组织。我不想创建一个可以使用return;,的新方法;,所以我做了一个示波器。现在我创建了一个新方法。将do放在{之前,而whilefalse放在}之后,break将起作用…使用return;关键字您可以使用“中断”来中断循环或切换。@Alberto,这将永远不允许执行此处最后的//执行内容。@Fildor这只是代码组织。我不想创建一个可以使用return;,的新方法;,所以我做了一个示波器。现在我创建了一个新方法。