How to Convert an Array to a List in C#?

Introduction

The following code example explains converting an array to a List in C#. The code example is written in C# 10 and .NET 6.0. 

There are multiple ways to convert an array to a list in C#. One method is using a List.AddRange method that takes an array as input and adds all array items to a List. The second method is using the ToList method of collection. 

// C# Array to List
Console.WriteLine("C# Array to List Example!");

// Array of string items
string[] names = { "Mahesh Chand", "Doug Wagner", "Neel Beniwal" };

// Covert array to List #1
Console.WriteLine("----AddRange method----");
List<string> list = new();
list.AddRange(names);
foreach(string name in names)
    Console.WriteLine(name);


// Convert array to List #2
Console.WriteLine("----ToList method----");
List<string> namesList = names.ToList();
foreach (string name in namesList)
    Console.WriteLine(name);

Console.ReadKey();


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.