How to break out of multiple loops at once in C#?

前端 未结 4 1166
遥遥无期
遥遥无期 2021-02-02 04:54

What if I have nested loops, and I want to break out of all of them at once?

while (true) {
    // ...
    while (shouldCont) {
        // ...
        while (sho         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-02 05:36

    If you want to break out of an entire method, then use the code below. If you only want to break out of a series of loops within a method without breaking out of the method, then one of the answers that have already been posted will do the job.

    if (TimeToStop)
    {
       return;
    }
    

提交回复
热议问题