C# Take, TakeWhile Extension Methods

Use the Take and TakeWhile extension methods to get the first several elements of an IEnumerable.
Take returns the first specified number of elements. It acts upon an IEnumerable sequence. It returns another IEnumerable sequence containing the specified elements. We see basic usages of this extension method.Extension
Example. Here we use the Take extension method. Take is a method in the System.Linq namespace that allows you to get the first several elements from a sequence. It receives an element count.LINQ

Call 1: In this example, the first Take call returns the first two elements in the List. This could display the two oldest, first elements.

Call 2: The second Take call is chained after a Reverse<string> call. It operates on the result of Reverse.


Note: This part will print out the last two elements in Reverse order. This could be used to display the newest elements.

And: The final Take call uses the top 1000 elements, which is nonsense as there are not 1000 elements in the List.

C# program that uses Take using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { List<string> list = new List<string>(); list.Add("cat"); list.Add("dog"); list.Add("programmer"); // Get first 2 elements var first = list.Take(2); foreach (string s in first) { Console.WriteLine(s); } Console.WriteLine(); // Get last 2 elements reversed var last = list.Reverse<string>().Take(2); foreach (string s in last) { Console.WriteLine(s); } Console.WriteLine(); // Get first 1000 elements var all = list.Take(1000); foreach (string s in all) { Console.WriteLine(s); } Console.WriteLine(); } } Output cat dog programmer dog cat dog programmer
TakeWhile. TakeWhile returns elements while a Predicate matches. The Predicate returns true or false. When it returns false, TakeWhile returns. TakeWhile operates from the beginning of an IEnumerable collection.Predicate

Next: In this program, an integer array of five elements is declared. The first three numbers in it are odd, while the last two are even.

Odd, Even

And: The TakeWhile extension method is invoked. It returns the first three odd numbers, but not the even numbers at the end.

C# program that uses TakeWhile method using System; using System.Linq; class Program { static void Main() { // Contains five numbers. int[] values = { 1, 3, 5, 8, 10 }; // Take all non-even (odd) numbers. var result = values.TakeWhile(item => item % 2 != 0); // Display result. foreach (int value in result) { Console.WriteLine(value); } } } Output 1 3 5
Reading the lambda expression. How can you read the lambda expression? The name "item" is just a temporary variable name. Each element becomes an item, and then the function returns true only if the value is not evenly divisible by two.LambdaModulo
Discussion. Skip is useful when paired with Take. It allows you to avoid the first several elements in the sequence. When you combine Skip with Take, you can indicate elements that you want to avoid based on a numeric pattern.Skip, SkipWhile
Summary. Take() gets elements from a collection. Also, we saw Reverse with Take, and how to use simple extension methods. Take method performance is good, but it is not as fast as custom extension methods may be.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls