CSharp - LINQ SkipWhile


SkipWhile operator will process an input sequence, skipping elements while a condition is true.

And it will then yield the remaining elements into an output sequence.


There are two prototypes for the SkipWhile operator we will cover.

The First SkipWhile Prototype

public static IEnumerable<T> SkipWhile<T>(
  this IEnumerable<T> source,
  Func<T, bool> predicate);

The SkipWhile operator of the above prototype accepts an input source sequence and a predicate method delegate.

And it returns an object that, when enumerated, skips elements while the predicate method returns true.

Once the predicate method returns false, the SkipWhile operator yields all subsequent elements.

The predicate method receives one element at a time from the input sequence and returns whether the element should be skipped in the output sequence.

SkipWhile has a second prototype that looks like this:

public static IEnumerable<T> SkipWhile<T>(
  this IEnumerable<T> source,
  Func<T, int, bool> predicate);

This prototype will be passed a zero-based index of the element in the input source sequence.


ArgumentNullException is thrown if any arguments are null.


using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Program//w w  w .j  a  v  a 2s.  c  o m
    static void Main(string[] args)
          string[] codeNames = {"Python", "Java", "Javascript", "Bash", "C++", "Oracle"};
          IEnumerable<string> items = codeNames.SkipWhile(s => s.StartsWith("P"));
          foreach (string item in items)


In the code above SkipWhile method skips elements as long as they started with the string "P".

All the remaining elements will be yielded to the output sequence.

Related Topics