List<string> liste = new List<String>
{
"A","B","C","D"
};
foreach (var item in liste)
{
System.Diagnostics.Debug.WriteLine(item.ToString());
}
for (int i = 0; i < liste.Count; i++)
{
if (i == 0)
continue;
System.Diagnostics.Debug.WriteLine(liste[i].ToString());
}
How do i skip a specific position in a foreach loop? I do not want to evaluate any values, but just skip the position x.
It has to be a specific position. One could choose position 0 or maybe position 7.
It is very easy to skip the first item in the list:
If you want to skip any other element at index
n, you could write this:In this example I use a lambda expression that takes two arguments:
aandb. Argumentais the item itself, while argumentbis the index of the item.The relevant pages on MSDN that describe these extension methods are:
You could even write your own extension method that allows you to skip an element in a list:
This will allow you to write something like this to skip an item: