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
TakeWhile method in C# ()
The TakeWhile() method in C# is a LINQ extension method that returns elements from the beginning of a sequence as long as a specified condition is true. It stops immediately when the first element that doesn't satisfy the condition is encountered, even if later elements might satisfy the condition.
Syntax
Following is the syntax for the TakeWhile() method −
public static IEnumerable<TSource> TakeWhile<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate
)
Parameters
-
source − The sequence to return elements from.
-
predicate − A function to test each element for a condition.
Return Value
Returns an IEnumerable<TSource> containing the elements from the input sequence that occur before the element at which the test no longer passes.
Using TakeWhile with Numbers
The following example demonstrates how TakeWhile() returns elements from the beginning of an array until the condition fails −
using System;
using System.Linq;
public class Demo {
public static void Main() {
int[] arr = { 25, 40, 65, 70 };
var val = arr.TakeWhile(ele => ele < 30);
Console.WriteLine("Elements taken while less than 30:");
foreach (int res in val) {
Console.WriteLine(res);
}
}
}
The output of the above code is −
Elements taken while less than 30: 25
Understanding Sequential Processing
It's important to understand that TakeWhile() stops at the first element that fails the condition, regardless of subsequent elements. Here's an example that demonstrates this behavior −
using System;
using System.Linq;
public class Demo {
public static void Main() {
int[] numbers = { 1, 3, 5, 8, 2, 4, 6 };
var result = numbers.TakeWhile(x => x < 6);
Console.WriteLine("Original array: " + string.Join(", ", numbers));
Console.WriteLine("TakeWhile(x => x < 6):");
foreach (int num in result) {
Console.WriteLine(num);
}
}
}
The output of the above code is −
Original array: 1, 3, 5, 8, 2, 4, 6 TakeWhile(x => x < 6): 1 3 5
Using TakeWhile with Strings
The TakeWhile() method works with any enumerable collection. Here's an example with strings −
using System;
using System.Linq;
public class Demo {
public static void Main() {
string[] words = { "apple", "banana", "cherry", "date", "elderberry" };
var result = words.TakeWhile(word => word.Length < 6);
Console.WriteLine("Words with length less than 6:");
foreach (string word in result) {
Console.WriteLine(word);
}
}
}
The output of the above code is −
Words with length less than 6: apple
TakeWhile vs Where
| TakeWhile | Where |
|---|---|
| Stops at the first element that fails the condition | Filters all elements throughout the entire sequence |
| Sequential processing from the beginning | Examines every element in the collection |
| Returns elements until condition becomes false | Returns all elements that satisfy the condition |
Conclusion
The TakeWhile() method is useful for extracting elements from the beginning of a sequence based on a condition. Unlike filtering methods like Where(), it stops processing as soon as the first element fails the condition, making it efficient for scenarios where you need consecutive elements that meet specific criteria.
