Convert Array to List in C#

  1. Convert an Array to a List With the Array.ToList() Method Inside Linq in C#
  2. Convert an Array to a List With the List.AddRange() Method in C#

This tutorial will discuss methods to convert an array to a list in C#.

Convert an Array to a List With the Array.ToList() Method Inside Linq in C#

The Linq or language integrated query is used for fast text manipulation in C#. The Array.ToList() method inside the Linq can convert an array to a list. The Array.ToList() method converts the calling array to a list and returns the result in a list data structure. The following code example shows us how to convert an array to a list with the Array.ToList() method inside the Linq in C#.

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

namespace convert_array_to_list
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { 10, 12, 13 };

            List<int> lst = arr.ToList();
            foreach (var element in lst)
            {
                Console.WriteLine(element);
            }
        }
    }
}

Output:

10
12
13

Convert an Array to a List With the List.AddRange() Method in C#

The List.AddRange() method is used to insert a range of values inside a list in C#. The List.AddRange() inserts the element of any data structure inside the calling list. The following code example shows us how to convert an array to a list with the List.AddRange() function in C#.

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

namespace convert_array_to_list
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { 10, 20, 30 };

            List<int> lst = new List<int>();
            lst.AddRange(arr);
            foreach (var element in arr)
            {
                Console.WriteLine(element);
            }
        }
    }
}

Output:

10
20
30
Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C# Array

  • Get the Length of an Array in C#
  • Sort an Array in C#
  • Sort an Array in Descending Order in C#
  • Remove Element of an Array in C#
  • Related Article - C# List

  • Get the Length of an Array in C#
  • Sort an Array in C#
  • Sort an Array in Descending Order in C#
  • Remove Element of an Array in C#