Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
SkipWhile method in C#
The SkipWhile method in C# is a LINQ extension method that skips elements from the beginning of a sequence as long as a specified condition is true. It stops skipping once it encounters the first element that doesn't meet the condition and returns all remaining elements, including any subsequent elements that might match the condition again.
This method is particularly useful when you need to skip a consecutive sequence of elements from the start of a collection based on a specific criteria.
Syntax
Following is the syntax for the SkipWhile method −
public static IEnumerable<TSource> SkipWhile<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate
)
There is also an overload that includes the element's index −
public static IEnumerable<TSource> SkipWhile<TSource>(
this IEnumerable<TSource> source,
Func<TSource, int, bool> predicate
)
Parameters
source: The sequence to skip elements from.
predicate: A function that tests each element for a condition.
Return Value
Returns an IEnumerable<T> containing the elements from the input sequence starting from the first element that doesn't satisfy the predicate condition.
Using SkipWhile with Even Numbers
The following example skips all even elements from the beginning of an array −
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 from the beginning
var res = arr.SkipWhile(ele => ele % 2 == 0);
Console.WriteLine("New array after skipping even elements...");
foreach (int val in res) {
Console.WriteLine(val);
}
}
}
The output of the above code is −
Initial array... 20 35 55 New array after skipping even elements... 35 55
Using SkipWhile with String Length
This example demonstrates skipping strings from the beginning while their length is less than 5 characters −
using System;
using System.Linq;
public class StringDemo {
public static void Main() {
string[] names = { "Jo", "Sam", "Alice", "Robert", "Kate" };
Console.WriteLine("Original names:");
foreach (string name in names) {
Console.WriteLine(name);
}
var result = names.SkipWhile(name => name.Length < 5);
Console.WriteLine("\nAfter skipping names with length < 5:");
foreach (string name in result) {
Console.WriteLine(name);
}
}
}
The output of the above code is −
Original names: Jo Sam Alice Robert Kate
Using SkipWhile with Index
This example shows how to use the overload that provides the element's index in the predicate −
using System;
using System.Linq;
public class IndexDemo {
public static void Main() {
int[] numbers = { 10, 20, 30, 40, 50 };
Console.WriteLine("Original numbers:");
foreach (int num in numbers) {
Console.WriteLine(num);
}
// Skip elements while their value is greater than their index * 15
var result = numbers.SkipWhile((value, index) => value > index * 15);
Console.WriteLine("\nAfter skipping based on index condition:");
foreach (int num in result) {
Console.WriteLine(num);
}
}
}
The output of the above code is −
Original numbers: 10 20 30 40 50 After skipping based on index condition: 30 40 50
Conclusion
The SkipWhile method in C# skips elements from the beginning of a sequence while a condition remains true and returns all remaining elements once the condition fails. It's important to understand that it only skips consecutive elements from the start, not all elements that match the condition throughout the sequence.
