C# Convert ArrayList to Array

by Sam Allen - Updated January 7, 2010

You have an ArrayList instance in your C# program and want to copy it or convert it to an array. Arrays and constructed types are more common in today's programs, but the ArrayList is still in wide usage in older programs. Here we look at how you can use the ToArray instance method on the ArrayList type, and then peek inside the internals of the base class library to see the algorithmic complexity of the method.

Array

Converting ArrayList to array

First, you will need to provide a Type parameter to the ToArray method on ArrayList. This is because otherwise the method would not know what type to convert each element to. Here we see how you can do that with the typeof operator. Note that the string keyword is used, and not a string literal. Specifying types with "String Names" is less clear.

using System;
using System.Collections;

class Program
{
    static void Main()
    {
        //
        // Create an ArrayList with 4 strings.
        //
        ArrayList list = new ArrayList();
        list.Add("flora");
        list.Add("fauna");
        list.Add("mineral");
        list.Add("plant");
        //
        // Convert ArrayList to array.
        //
        string[] array = list.ToArray(typeof(string)) as string[];
        //
        // Loop over array.
        //
        foreach (string value in array)
        {
            Console.WriteLine(value);
        }
    }
}

The preceding example. The code defines the Main entry point and then populates a new ArrayList with four object instances containing string data (string literals). In this example, the strings are interned by the runtime, so share memory.

Note on the "as" cast. The "as" cast here is necessary to use strong typing on the result from ToArray. It will result in a null value if the cast doesn't succeed for whatever reason. In some programs, it is best to check for null.

Internal implementation

When you open up the ToArray instance method on ArrayList in the Red Gate Reflector, you will see that this method, along with all the ToArray and CopyTo methods on ArrayList, call into the Array.Copy static method. Array.Copy then goes to an external, native-code implementation. This provides superior performance over manually copying elements in your C# program.

(See Array.Copy Method Usage.)

Summary

Here we looked at how you can use the ArrayList's ToArray method to convert the contents of an ArrayList to a string array. The example here can be adapted to other reference and value types. Additionally, we looked inside the base class library and found that the method is implemented as a call to Array.Copy. This article is based on the .NET Framework 3.5 SP1.

(Do not copy this page.)

Dot Net Perls | Search
Conversion | Convert Bool to Int | Convert Char Array to String | Convert List to Array | Convert String Array to String | List and String Conversion
C# | Parameter Optimization Tip | SaveFileDialog Tutorial | IntegralHeight Property (Windows Forms) | Array.FindIndex Method
© 2010 Sam Allen. All rights reserved.
Dot Net Perls | Sam Allen