SkipWhile method in C#


SkipWhile skips an element when a condition is matched.

For example, use the following if you want to skip all even elements −

ele => ele %2 == 0

The following is an example wherein all the even elements are skipped and only the odd elements are displayed −

Example

 Live Demo

using System.IO;
using System;
using System.Linq;
public class Demo {
   public static void Main() {
      int[] arr = { 20, 35, 55 };
      Console.WriteLine("Initial array...");
      foreach (int value in arr) {
         Console.WriteLine(value);
      }
      // skipping even elements
      var res = arr.SkipWhile(ele => ele % 2 == 0);
      Console.WriteLine("New array after skipping even elements...");
      foreach (int val in res) {
         Console.WriteLine(val);
      }
   }
}

Output

Initial array...
20
35
55
New array after skipping even elements...
35
55

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 22-Jun-2020

261 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements