Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip a specific position within a for each loop in c sharp?

Tags:

c#

loops

foreach

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.

like image 460
Gero Avatar asked Oct 29 '11 23:10

Gero


People also ask

How do you skip elements in foreach loop?

if want to skipped some index then make an array with skipped index and check by in_array function inside the foreach loop if match then it will be skip.

How do you skip a loop in C#?

In C#, the continue statement is used to skip over the execution part of the loop(do, while, for, or foreach) on a certain condition, after that, it transfers the control to the beginning of the loop. Basically, it skips its given statements and continues with the next iteration of the loop.

What is for each loop in C sharp?

The foreach loop in C# iterates items in a collection, like an array or a list. It proves useful for traversing through each element in the collection and displaying them. The foreach loop is an easier and more readable alternative to for loop.


2 Answers

It is very easy to skip the first item in the list:

foreach(var item in list.Skip(1))
{
    System.Diagnostics.Debug.WriteLine(item.ToString());
}

If you want to skip any other element at index n, you could write this:

foreach(var item in list.Where((a,b) => b != n))
{
    System.Diagnostics.Debug.WriteLine(item.ToString());
}

In this example I use a lambda expression that takes two arguments: a and b. Argument a is the item itself, while argument b is the index of the item.

The relevant pages on MSDN that describe these extension methods are:

  • IEnumerable.Skip()
  • IEnumerable.Where()

You could even write your own extension method that allows you to skip an element in a list:

public static class MyEnumerableExtensions
{
    public static IEnumerable<T> SkipAt<T>(this IEnumerable<T> list, int index)
    {
        var i = 0;

        foreach(var item in list) 
        {
            if(i != index)
                yield return item;

            i++;
        }
    }
}

This will allow you to write something like this to skip an item:

foreach(var item in list.SkipAt(2))
{
    System.Diagnostics.Debug.WriteLine(item.ToString());
}
like image 193
Elian Ebbing Avatar answered Oct 19 '22 19:10

Elian Ebbing


A foreach loop iterates over a collection that implements IEnumerable. The enumerator exposes the current item and a method to move onto the next item - it has no concept of an index.

You could always do:

var i = 0;
foreach (var item in liste) {
  if (i++ == skip) continue;
  Debug.WriteLine(item.ToString());
}

But this seems unnecessarily contrived. If you need an index, go with a for loop.

The other option is to remove the undesired item from the List before iterating:

foreach (var item in liste.Take(n-1).Union(liste.Skip(n))) {
  Debug.WriteLine(item.ToString());
}
like image 1
James Avatar answered Oct 19 '22 18:10

James