C# Take, TakeWhile

LINQ: language integrated query

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 MethodIEnumerable

Example

Reverse

Here we use the Take extension method in the C# language. 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.

Here:We look at a couple simple examples of how Take works. Then we look at using Reverse with Take.

Reverse
Program that uses Take: C#

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
List type

In this example, the first Take call returns the first two elements in the List. This could display the two oldest, first elements. The second Take call above 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 above uses the top 1000 elements, which is nonsense as there are not 1000 elements in the List.

List

However:The final Take call shows that Take will manage bad parameters without a problem.

TakeWhile

Method call

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.

PredicateNote

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.

Program that uses TakeWhile method: C#

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
Question and answer

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.

Lambda ExpressionModulo

Discussion

Programming tip

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

C# programming language

We saw examples of using the Take method in the C# language. Also, we saw Reverse with Take, and how to use simple extension methods. Take method performance is fairly good, but it is not as fast as custom extension methods may be.

Therefore:Take is not suitable for performance-critical applications. It is helpful in higher-level programs.


C#: LINQ