How to Convert Array to List in C#

  1. Using Array.ToList() Function
  2. Using List.AddRange() Function
  3. Conclusion
  4. FAQ
How to Convert Array to List in C#

Converting an array to a list in C# is a common task that many developers encounter. Whether you’re working on a small project or a large application, knowing how to manipulate data structures efficiently is essential. In C#, there are two primary methods to convert an array into a list: using the Array.ToList() function from LINQ and the List.AddRange() function. Both methods have their advantages and can be applied in different scenarios depending on your needs.

In this article, we’ll explore these methods in detail, providing clear examples and explanations to help you understand how to implement them effectively.

Using Array.ToList() Function

The Array.ToList() method is part of the LINQ (Language Integrated Query) namespace in C#. This method allows you to easily convert an array into a list with minimal code. It is straightforward and efficient, making it a popular choice among developers. To use this method, you first need to ensure you have included the System.Linq namespace in your project.

Here’s how you can convert an array to a list using the Array.ToList() method:

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

class Program
{
    static void Main()
    {
        int[] numbers = { 1, 2, 3, 4, 5 };
        List<int> numberList = numbers.ToList();

        foreach (var number in numberList)
        {
            Console.WriteLine(number);
        }
    }
}

Output:

1
2
3
4
5

In this example, we first declare an array of integers called numbers. By invoking the ToList() method on the numbers array, we create a new list called numberList. The foreach loop then iterates through the list, printing each number to the console. This method is concise and effective, especially for developers who prefer a clean syntax. However, keep in mind that this approach requires the System.Linq namespace, which may add overhead if you’re not already using LINQ in your project.

Using List.AddRange() Function

Another method to convert an array to a list in C# is by using the List.AddRange() function. This method allows you to add multiple elements to a list in one go, making it a flexible option for converting arrays. Unlike the ToList() method, AddRange() requires you to first create an instance of the list and then add the array elements to it.

Here’s how you can use the List.AddRange() function to convert an array into a list:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        string[] fruits = { "Apple", "Banana", "Cherry" };
        List<string> fruitList = new List<string>();

        fruitList.AddRange(fruits);

        foreach (var fruit in fruitList)
        {
            Console.WriteLine(fruit);
        }
    }
}

Output:

Apple
Banana
Cherry

In this example, we declare an array of strings called fruits. We then create an empty list named fruitList. By using the AddRange() method, we add all elements of the fruits array to the fruitList. The foreach loop subsequently prints each fruit to the console. This method is particularly useful when you want to manipulate the list further after its creation or when you need to add elements from multiple arrays or collections.

Conclusion

In summary, converting an array to a list in C# can be accomplished using two main methods: the Array.ToList() function from LINQ and the List.AddRange() function. Each method has its unique advantages, allowing you to choose the one that best suits your project’s needs. The ToList() method is concise and straightforward, while AddRange() offers more flexibility for further list manipulation. Understanding these methods will enhance your ability to work with collections in C#, making your code cleaner and more efficient.

FAQ

  1. What is the difference between Array.ToList() and List.AddRange()?
    Array.ToList() creates a new list directly from an array, while List.AddRange() requires you to create a list first and then add array elements to it.

  2. Do I need to use System.Linq to use Array.ToList()?
    Yes, you must include the System.Linq namespace in your project to use the Array.ToList() method.

  3. Can I convert a multi-dimensional array to a list?
    Both methods discussed are designed for one-dimensional arrays. For multi-dimensional arrays, you will need to flatten the array first before converting it to a list.

  4. Is there a performance difference between the two methods?
    Generally, Array.ToList() is more concise and may be faster for small arrays, while List.AddRange() can be more flexible for larger datasets or when combining multiple arrays.

  5. Can I convert arrays of different types to lists?
    Yes, you can convert arrays of any type (integers, strings, custom objects) to lists using either method, as long as the list is of the same type.

using two main methods: Array.ToList() from LINQ and List.AddRange(). This article provides clear examples and detailed explanations to help you understand these methods effectively. Enhance your C# skills with these efficient techniques for data structure manipulation.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
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