How do I skip an iteration of a `foreach` loop?

后端 未结 8 1300
天涯浪人
天涯浪人 2020-12-22 16:09

In Perl I can skip a foreach (or any loop) iteration with a next; command.

Is there a way to skip over an iteration and jump to the next loop in C#?

8条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-22 16:57

    Another approach using linq is:

    foreach ( int number in numbers.Skip(1))
    {   
        // process number  
    }
    

    If you want to skip the first in a number of items.

    Or use .SkipWhere if you want to specify a condition for skipping.

提交回复
热议问题