How To Convert An ArrayList To An Array In C Sharp - C Sharp ArrayList Example Series

103 49
ArrayList is a very useful data structure in C#.
Although since.
NET framework 2.
0, programmers have a better choice (Generic List) to help them in dealing with a list of objects, ArrayList is still quite useful because it supports some functionality in some cases which Generic List does not support, as well as it has better performance in some occasions; Therefore, I will post a series examples and tips to show how to use ArrayList in C#, especially for C# beginners and programmers who are not familiar with ArrayList.
Today our topic is using how to convert an ArrayList to an array in C#.
Net.
As I mentioned in the previous post of this C# ArrayList series, ArrayList (or Generic List) is dynamic and more fixable than traditional array, so we should always use them to create and maintain a list of objects.
But in some situation, we still need traditional array, Especially when we access some interface which only accept an array as its parameter.
For this case, we need convert an ArrayList to an array.
In C#.
Net there are two methods to help us to deal with the conversion: public virtual Array ToArray(Type type) and its overload version: public virtual Object[] ToArray() If we know the type of items in the ArrayList, then we should use the 1st one, otherwise use 2nd one.
Assuming we have a string ArrayList stringArrayList, now we want to convert it to a string[], the code should be like this: String[] myArr = (String[])stringArrayList.
ToArray( typeof( string ) ); The usage of method ToArray is quite simple and straightforward.
Now, let's see a little complex example: // The code is simplified from MSDN, // and has been tested on vs2010 with.
Net 4 by Mavis using System; using System.
Collections; public class SamplesArrayList { public static void Main() { // Creates and initializes a new ArrayList.
ArrayList stringArrayList = new ArrayList(); stringArrayList.
Add("The"); stringArrayList.
Add("quick"); stringArrayList.
Add("brown"); stringArrayList.
Add("fox"); stringArrayList.
Add("jumped"); stringArrayList.
Add("over"); stringArrayList.
Add("the"); stringArrayList.
Add("lazy"); stringArrayList.
Add("dog"); // Displays the values of the ArrayList.
Console.
WriteLine("The ArrayList contains the following values:"); PrintIndexAndValues(stringArrayList); // Copies the elements of the ArrayList to a string array.
String[] stringArray = (String[])stringArrayList.
ToArray(typeof(string)); // Displays the contents of the string array.
Console.
WriteLine("The string array contains the following values:"); PrintIndexAndValues(stringArray); } public static void PrintIndexAndValues(ArrayList myList) { int i = 0; foreach (Object o in myList) Console.
WriteLine("t[{0}]:t{1}", i++, o); Console.
WriteLine(); } public static void PrintIndexAndValues(String[] stringArray) { for (int i = 0; i < stringArray.
Length; i++) Console.
WriteLine("t[{0}]:t{1}", i, stringArray[i]); Console.
WriteLine(); } } /* This code produces the following output.
The ArrayList contains the following values: [0]: The [1]: quick [2]: brown [3]: fox [4]: jumped [5]: over [6]: the [7]: lazy [8]: dog The string array contains the following values: [0]: The [1]: quick [2]: brown [3]: fox [4]: jumped [5]: over [6]: the [7]: lazy [8]: dog */ Here are some a bit advanced discussion that you may need to take care when using ToArray method.
The results of below to lines are same, but the performance is different: object[] stringArray = (object[])stringArrayList.
ToArray(typeof(object)); object[] stringArray = stringArrayList.
ToArray(); Because the 1st line ToArray uses reflection to create a new object[] which is quite time-consuming; while 2nd line ToArray create object[] directly, which is much faster.
In addition to that, we also need to know that actually what ToArray method does is not conversion; while it creates a new array and copy items from ArrayList to the new created array.
So the operation is quite expensive, for this reason we should only call ToArray when we have to.
Try the code yourself and leave comments at my blog Convert ArrayList To Array.
Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.