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#
Convert Array to List 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
Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn

Related Article - Csharp 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#
  • Convert a String to a Byte Array in C#
  • Adding Values to a C# Array
  • Related Article - Csharp List

  • Convert an IEnumerable to a List in C#
  • C# Remove Item From List
  • C# Join Two Lists Together
  • Get the First Object From List<Object> Using LINQ
  • Find Duplicates in a List in C#
  • The AddRange Function for List in C#