ToList and ToArray Methods

ToList and ToArray Methods in C# with Examples

In this article, I am going to discuss the Linq ToList and ToArray Methods in C# with some examples. Please read our previous article where we discussed the difference between Deferred Execution vs Immediate Execution in LINQ with some examples. The ToList and ToArray methods belong to the conversion operator category.

ToList Method in LINQ:

The ToList Method is used to create a System.Collections.Generic.List<T> collection from a System.Collections.Generic.IEnumerable<T>. This method causes the query to be executed immediately.

The signature of the ToList method is given below.

ToList and ToArray Methods in Linq with Examples

Parameters:
source: The data type of source is System.Collections.Generic.IEnumerable<T>.

Type parameters:

TSource: The type of the elements contained in the source sequence.

Returns: It returns System.Collections.Generic.List<T> which contains elements from the source sequence.

Exceptions: This method throws System.ArgumentNullException when the source sequence is null.

Example: Convert int array to List<int> using the ToList method

In the following example, first, we create an integer array and then we convert that array into a list by using the ToList method.

using System.Collections.Generic;
using System.Linq;
using System;

namespace ConversionOperators
{
    class Program
    {
        public static void Main()
        {
            //Creating Integer Array
            int[] numbersArray = { 10, 22, 30, 40, 50, 60 };

            //Converting Integer Array to List using ToList method
            List<int> numbersList = numbersArray.ToList();

            foreach (var num in numbersList)
            {
                Console.WriteLine(num);
            }

            Console.ReadKey();
        }
    }
}
ToArray Method in Linq:

The ToArray Method is used to copies the elements of System.Collections.Generic.List<T> to a new array. This method causes the query to be executed immediately. The signature of this method is given below.

Linq ToArray Method

Here T is the type of the array and this method converts a list into an array and returns that array containing copies of the elements of the System.Collections.Generic.List<T>.

Example: Convert List<int> to integer array. 

In the following example, first, we create a list of integers and then convert that list into an integer array by using the ToArray method.

using System.Collections.Generic;
using System;

namespace ConversionOperators
{
    class Program
    {
        public static void Main()
        {
            //Create a List
            List<int> numbersList = new List<int>()
            {
                10, 22, 30, 40, 50, 60
            };

            //Converting List to Array
            int[] numbersArray = numbersList.ToArray();

            foreach (var num in numbersArray)
            {
                Console.WriteLine(num);
            }

            Console.ReadKey();
        }
    }
}

That’s it for today. In the next article, I am going to discuss the ToDictionary method in C# with some examples. Here, in this article, I try to explain the need and use of ToList and ToArray Methods with some examples. I hope you understood the use of ToList and ToArray method in C#.

Leave a Reply

Your email address will not be published. Required fields are marked *