ToList and ToArray Methods

Introduction:

In C#, the ToList and ToArray methods are useful tools for converting a collection into a list or an array, respectively. Both methods are LINQ extension methods that are available on any object that implements the IEnumerable interface. In this lesson, we will explore the ToList and ToArray methods in detail, including their definitions, syntax, and examples.

Definition:

The ToList and ToArray methods are LINQ extension methods that are used to convert a collection into a list or an array, respectively. Both methods create a new instance of a list or an array and populate it with the elements of the source collection. The resulting list or array contains the same elements as the source collection and is a separate instance from the source collection.

Syntax:

The syntax for the ToList and ToArray methods is as follows:

List<T> ToList<T>(this IEnumerable<T> source)
T[] ToArray<T>(this IEnumerable<T> source)

Both methods take a single parameter, which is the source collection to be converted. The generic type parameter, T, specifies the type of the elements in the resulting list or array.

Example 1: Converting a Collection to a List

Let’s say we have an array of integers, and we want to convert it to a list. We can use the ToList method to accomplish this as follows:

int[] numbers = { 1, 2, 3, 4, 5 };
List<int> numberList = numbers.ToList();

In this example, we first create an array of integers called numbers. We then use the ToList method to create a new List called numberList, which contains the same elements as the numbers array.

Example 2: Converting a Collection to an Array

Let’s say we have a list of strings, and we want to convert it to an array. We can use the ToArray method to accomplish this as follows:

List<string> names = new List<string>() { "John", "Jane", "Bob" };
string[] nameArray = names.ToArray();

In this example, we first create a List of strings called names. We then use the ToArray method to create a new string array called nameArray, which contains the same elements as the names List.

Example 3: Customizing the Output

Both the ToList and ToArray methods can be customized to include only certain elements from the source collection or to transform the elements in some way. For example, we can use the Where method to include only even numbers from an array of integers and then convert the resulting collection to a list as follows:

int[] numbers = { 1, 2, 3, 4, 5 };
List<int> evenNumbers = numbers.Where(n => n % 2 == 0).ToList();

In this example, we first create an array of integers called numbers. We then use the Where method to include only even numbers in the array and create a new IEnumerable<int> containing these even numbers. Finally, we use the ToList method to convert the resulting collection to a List called evenNumbers.

Conclusion:

The ToList and ToArray methods are useful tools for converting a collection into a list or an array in C#. By understanding how to use these methods and their syntax, developers can easily create lists or arrays from any object that implements the IEnumerable interface. These methods can simplify code and improve performance, especially when working with large collections. By customizing the output, developers can create lists or arrays that fit their specific needs.