C# Enumerable SkipWhile(IEnumerable, Func)

Description

Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements. The element's index is used in the logic of the predicate function.

Syntax


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

Parameters

  • TSource - The type of the elements of source.
  • source - An IEnumerable to return elements from.
  • predicate - A function to test each source element for a condition; the second parameter of the function represents the index of the source element.

Example

The following code example demonstrates how to use SkipWhile to skip elements of an array as long as a condition that depends on the element's index is true.


/*w w w  .  j a va2s.c  o  m*/
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
  public static void Main(String[] argv){  

    int[] amounts = { 5000, 2500, 9000, 8000, 
                        600, 4000, 500, 5500 };

    IEnumerable<int> query =
        amounts.SkipWhile((amount, index) => amount > index * 1000);

    foreach (int amount in query)
    {
        Console.WriteLine(amount);
    }
  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Linq »




Enumerable