Back
Close

Using C# LINQ - A Practical Overview

player_one
69.3K views
Previous: IEnumerable<T> Next: Using LINQ

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();
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants