C# equivalent to Java's continue

前端 未结 5 1755
南笙
南笙 2020-12-14 06:13

Should be simple and quick: I want a C# equivalent to the following Java code:

orig: for(String a : foo) {
  for (String b : bar) {
    if (b.equals(\"buzz\"         


        
5条回答
  •  一生所求
    2020-12-14 07:03

    I don't believe there's an equivalent, I'm afraid. You'll have to either use a boolean, or just "goto" the end of the inside of the outer loop. It's even messier than it sounds, as a label has to be applied to a statement - but we don't want to do anything here. However, I think this does what you want it to:

    using System;
    
    public class Test
    {
        static void Main()
        {
            for (int i=0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                   Console.WriteLine("i={0} j={1}", i, j);
                   if (j == i + 2)
                   {
                       goto end_of_loop;   
                   }
                }
                Console.WriteLine("After inner loop");
                end_of_loop: {}
            }
        }
    }
    

    I would strongly recommend a different way of expressing this, however. I can't think that there are many times where there isn't a more readable way of coding it.

提交回复
热议问题