How to Convert Array to List in C#

Muhammad Maisam Abbas Feb 16, 2024
  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#
How to 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

Related Article - Csharp List