is there a equivalent of Java's labelled break in C# or a workaround

后端 未结 1 514
攒了一身酷
攒了一身酷 2020-12-17 08:30

I am converting some Java code to C# and have found a few labelled \"break\" statements (e.g.)

label1:
    while (somethingA) {
       ...
        while (som         


        
相关标签:
1条回答
  • 2020-12-17 08:48

    You can just use goto to jump directly to a label.

    while (somethingA)
    {
        // ...
        while (somethingB)
        {
            if (condition)
            {
                goto label1;
            }
        }
    }
    label1:
       // ...
    

    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.

    0 讨论(0)
提交回复
热议问题