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.

Syntax


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

Parameters

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

Example

The following code example demonstrates how to use SkipWhile to skip elements of an array as long as a condition is true.


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

    int[] grades = { 529, 822, 720, 526, 922, 928, 825 };

    IEnumerable<int> lowerGrades =
        grades
        .OrderByDescending(grade => grade)
        .SkipWhile(grade => grade >= 80);

    Console.WriteLine("All grades below 80:");
    foreach (int grade in lowerGrades)
    {
        Console.WriteLine(grade);
    }
  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Linq »




Enumerable