Convert ArrayList to Array of the specified element type in CSharp

Description

The following code shows how to convert ArrayList to Array of the specified element type.

Example


using System; /*from   w w w.  ja  v  a  2 s.c  om*/
using System.Collections;
class MainClass
{
    public static void Main()
    {
        ArrayList list = new ArrayList(5);
        list.Add("B");
        list.Add("G");
        list.Add("J");
        list.Add("S");
        list.Add("M");

        string[] array3 = (string[])list.ToArray(typeof(String));
        
        foreach (string s in array3)
        {
            Console.WriteLine(s);
        }
     }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Collections »




ArrayList
BitArray
Collection
Comparer
HashSet
Hashtable
LinkedList
List
ListDictionary
OrderedDictionary
Queue
SortedList
SortedSet
Stack
StringCollection
StringDictionary