c sharp

How to Convert Array to a List in C#

The two most popular data structures in C# for storing collections of items are arrays and lists. While arrays are fixed in size and difficult to resize, lists offer more flexibility as they can be resized and have additional methods to manipulate the data. If you have an array and need to convert it to a list in C#, there are a few approaches you can take. This article will explore the different methods available to convert an array to a list in C#.

Methods to Convert an Array to a List in C#

In C#, there are several techniques to convert an array to a list, including:

Let’s explain these methods one by one.

1: Using List.AddRange() Method

To convert an array to a list in C#, you can create a new list and then use the AddRange() method of the list to add the elements from the original array.

Let’s take a similar example and use the AddRange() method to convert the array to a list.

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        string[] arr = { "L", "i", "n", "u", "x", "H", "i", "n", "t" };
        List<string> myList = new List();
        myList.AddRange(arr);

        Console.WriteLine("The converted list contains:");
        foreach(string character in myList)
        {
            Console.Write(character);
        }
    }
}

The above program initializes a string array, creates an empty list of strings, adds the array elements to the list using AddRange(), and then prints the list to the console.

2: Using Array.ToList() Method Inside LINQ

Language-integrated query, or LINQ, is a robust C# utility for rapidly manipulating text. One of the methods that LINQ provides is Array.ToList(), which can convert the array to the list in a single line of code. This function takes the given array and converts it to a list, returning the result in the list data structure.

Use of the Array.ToList() is demonstrated in the following piece of code. An array can be turned into a list using the C# language’s Array.ToList() function of LINQ.

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

class Program
{
    static void Main()
    {
        string[] arr = { "L", "i", "n", "u", "x", "H", "i", "n", "t" };

        List<string> myList = arr.ToList();

        Console.WriteLine("The converted list contains:");
        foreach(string character in myList)
        {
            Console.Write(character);
        }
    }
}

The above code demonstrates the array to a list using conversion using the built-in ToList() method from the System.Linq namespace. It creates an array of strings, converts it to a List<string>, and then prints out the elements of the list using a foreach loop.

3: Using Add() Method

In C#, the Add() method is used to append an object to the end of a List. Interestingly, this method can also be utilized to convert an array to a List. The code snippet below illustrates how the Add() method can be utilized to accomplish this conversion task in C#.

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        string[] arr = { "L", "i", "n", "u", "x", "H", "i", "n", "t" };
        List<string> myList = new List();
        foreach (string character in arr)
        {
            myList.Add(character);
        }
        Console.WriteLine("The converted list contains:");
        foreach(string character in myList)
        {
            Console.Write(character);
        }
    }
}

The above code uses the System namespace and System.Collections.Generic namespace to convert a string array to a List<string> using a foreach loop and the Add method. The elements in the list are then printed to the console.

4: Using List Constructor

This constructor can be used to create a new instance of the List<T> class that has elements copied from the given collection and has enough space to accommodate the total number of elements copied. As a result, this additionally can convert an array to a list.

To convert an array into a list in C#, use the list constructor method as shown in the example code below.

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        string[] arr = { "L", "i", "n", "u", "x", "H", "i", "n", "t" };
        List<string> myList = new List(arr);
        Console.WriteLine("The converted list contains:");
        foreach(string character in myList)
        {
            Console.Write(character);
        }
    }
}

The above example converts a char array to a List<char> and outputs its elements to the console. The System namespace provides fundamental types and classes for the runtime environment, while the System.Collections.Generic namespace provides generic collection classes and interfaces.

Conclusion

The C# you can convert an array to a list using List.AddRange(), Array.ToList() within LINQ, the Add(), and the List constructor. Each method provides a unique solution to the conversion process and can be utilized based on specific needs. A deep understanding of all these methods improves programming skills.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.