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
C# Program to skip elements from a sequence as long as the specified condition is true
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 remains true. Once the condition becomes false, it returns all remaining elements including the first element that failed the condition.
Syntax
Following is the syntax for the SkipWhile() method −
public static IEnumerable<T> SkipWhile<T>(
this IEnumerable<T> source,
Func<T, bool> predicate
)
Parameters
-
source − The sequence of elements to skip from.
-
predicate − A function to test each element for the condition.
Return Value
Returns an IEnumerable<T> containing the elements from the input sequence starting from the first element that does not satisfy the condition.
Using SkipWhile() with Ordered Sequence
Example
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
int[] marks = { 35, 42, 48, 88, 55, 90, 95, 85 };
// First order descending, then skip elements >= 50
IEnumerable<int> selMarks = marks.AsQueryable()
.OrderByDescending(s => s)
.SkipWhile(s => s >= 50);
Console.WriteLine("Original array: " + string.Join(", ", marks));
Console.WriteLine("After OrderByDescending: " + string.Join(", ", marks.OrderByDescending(x => x)));
Console.WriteLine("After SkipWhile(s >= 50):");
foreach (int res in selMarks) {
Console.WriteLine(res);
}
}
}
The output of the above code is −
Original array: 35, 42, 48, 88, 55, 90, 95, 85 After OrderByDescending: 95, 90, 88, 85, 55, 48, 42, 35 After SkipWhile(s >= 50): 48 42 35
Using SkipWhile() with Original Order
Example
using System;
using System.Linq;
public class Program {
public static void Main() {
int[] numbers = { 10, 15, 25, 5, 35, 40, 8 };
// Skip elements while they are less than 30
var result = numbers.SkipWhile(n => n < 30);
Console.WriteLine("Original array: " + string.Join(", ", numbers));
Console.WriteLine("SkipWhile(n < 30):");
Console.WriteLine(string.Join(", ", result));
}
}
The output of the above code is −
Original array: 10, 15, 25, 5, 35, 40, 8 SkipWhile(n < 30): 35, 40, 8
Using SkipWhile() with String Length
Example
using System;
using System.Linq;
public class StringExample {
public static void Main() {
string[] words = { "Hi", "C#", "Java", "Python", "JavaScript", "Go" };
// Skip words while their length is less than 5
var longWords = words.SkipWhile(word => word.Length < 5);
Console.WriteLine("Original words: " + string.Join(", ", words));
Console.WriteLine("SkipWhile(length < 5):");
foreach (string word in longWords) {
Console.WriteLine(word);
}
}
}
The output of the above code is −
Original words: Hi, C#, Java, Python, JavaScript, Go SkipWhile(length < 5): Python JavaScript Go
Key Differences: SkipWhile vs Where
| SkipWhile() | Where() |
|---|---|
| Skips elements from the beginning until condition becomes false | Filters elements throughout the entire sequence |
| Stops skipping after first false condition | Continues filtering all elements |
| Position-dependent behavior | Position-independent behavior |
Conclusion
The SkipWhile() method skips elements from the beginning of a sequence while a condition is true, then returns all remaining elements. Unlike filtering methods, it stops evaluating the condition after the first false result, making it useful for skipping initial elements that meet certain criteria.
