Conversion from System.Array to List<T>: An In-Depth Analysis in C#

Nov 07, 2025 · Programming · 15 views · 7.8

Keywords: C# | Array Conversion | List<T> | LINQ | Type Safety

Abstract: This article provides a comprehensive exploration of various methods to convert System.Array to List<T> in C#, focusing on the combination of LINQ's OfType<T>() and ToList() methods, as well as direct List constructor usage in different scenarios. By comparing conversions between strongly-typed arrays and generic Arrays, and considering performance and type safety, it offers complete implementation solutions and best practices to help developers efficiently handle collection type conversions.

Introduction

In C# development, converting collection types is a common task, with the conversion from System.Array to List<T> being particularly important. System.Array, as the base class for all arrays, provides dynamic creation and manipulation capabilities but lacks the convenience of generic collections. For example, a user might create an array using Array.CreateInstance(typeof(int), 5) and set values:

Array ints = Array.CreateInstance(typeof(int), 5);
ints.SetValue(10, 0);
ints.SetValue(20, 1);
ints.SetValue(10, 2);
ints.SetValue(34, 3);
ints.SetValue(113, 4);

However, directly using ints.OfType<int>() does not yield a List<int>, as OfType<T>() returns an IEnumerable<T> that requires further processing. This article systematically analyzes conversion methods, emphasizing type safety and performance optimization.

Core Conversion Methods

Based on the Q&A data, best practices involve LINQ and constructors. First, using the OfType<T>().ToList() method:

using System.Linq;

int[] ints = new[] { 10, 20, 10, 34, 113 };
List<int> lst = ints.OfType<int>().ToList();

This method is suitable for strongly-typed arrays, but note that OfType<T>() filters out non-T type elements, ensuring type safety. However, for System.Array, type issues must be addressed first. Reference Article 1 points out that direct list construction requires type matching: var list = new List<int>(array) only works if array is an int[]; if it is an object[], a compilation error occurs because the compiler checks type constraints.

Handling Specific Scenarios with System.Array

When dealing with non-strongly-typed System.Array, as in the Q&A example, explicit conversion is necessary. Answer 3 emphasizes that the Array must be cast to a specific type array:

List<int> intList = ((int[])ints).ToList();

This method leverages internal optimizations in ToList(): if the input implements ICollection<T> (like an array), it uses the CopyTo method, avoiding enumeration and improving performance. Reference Article 2 illustrates complex scenarios, such as converting an object[] to a List<double> where elements might be int or double, using Array.ConvertAll with a delegate:

List<double> list = new List<double>(Array.ConvertAll(array, new Converter<object, double>(delegate (object x) { return ((IConvertible)x).ToDouble(null); })));

This ensures flexibility in type conversion, though the code is more verbose.

Alternative Approaches and Performance Considerations

Beyond LINQ, Answers 1 and 2 provide multiple constructor-based methods:

These methods are efficient in simple scenarios, but for dynamic System.Array, type conversion must be considered. Performance-wise, ToList() is optimal on strongly-typed arrays as it directly copies elements; whereas OfType<T>() involves filtering and may be slower. Reference Article 1 stresses that the compiler does not care about runtime specific values, only ensuring type correctness, so caution is needed when using generic methods.

Conclusion and Best Practices

The key to converting System.Array to List<T> lies in type handling. Recommended steps: 1) Confirm the actual type of the Array; 2) Use explicit casting or OfType<T>().ToList(); 3) Prefer constructors to leverage performance optimizations. Avoid manual conversions in loops to enhance code readability and efficiency. By combining LINQ and basic collection operations, developers can effectively address type conversion challenges.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.