Using C# LINQ - A Practical Overview

player_one
5,576 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Background Topics - ToList() and ToArray()

Any LINQ method that returns a sequence of elements returns it as an IEnumerable<T>. For many applications, it can be difficult to work with this interface, and it may be desirable to iterate this enumerable to either a list or an array. Both of these are more intuitive to use.

LINQ provides two very handy methods to do just this: ToList() and ToArray(). The method names are pretty self-explanatory. They "convert" an IEnumerable<T> to either a List<T> or an array of type T[].

Here are a few examples:

List<int> list = new[] {1, 2, 3}.ToList();
int[] array = new List<int> {1, 2, 3}.ToArray();
List<int> list2 = new SortedSet<int> {1, 2, 3}.ToList();
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content