Click here to Skip to main content
15,920,602 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello friends,

How can I convert an IList to an ArrayList using C#?

Please help
Posted
Updated 4-Jul-13 21:49pm
v3
Comments
bbirajdar 5-Jul-13 6:04am    
Why do you want to move back from the exciting world of generics to the ArrayList ?

 
Share this answer
 
Comments
Honey sree 5-Jul-13 3:48am    
Hello,
i tried out this method. But its not working.
Its showing a casting exception
Pheonyx 5-Jul-13 3:51am    
Okay, so let's see your code.. use the "improve Question" button and put in the area of code where you are doing this action. Do not dump your entire code base, just the method/function where you are trying to achieve this cast.
bbirajdar 5-Jul-13 6:03am    
+5.. Compensated
C#
List<string> l = new List<string>();
    l.Add("one");
    l.Add("two");
    l.Add("three");
    l.Add("four");
    l.Add("five");

    string[] s = l.ToArray();
    ArrayList myArrayList = new ArrayList(s.Length);
    myArrayList.AddRange(s);
 
Share this answer
 
v2
Comments
Honey sree 5-Jul-13 3:49am    
Hi,
Does this provids the same functionality as what an ArrayList do?
lukeer 5-Jul-13 5:08am    
What funcionality would that be?

Or, try it. Change one ArrayList to List<whatever> and see if it works.
First of all, you should design your code the way the "conversion" (but in this case this is not "conversion" but type cast from interface type to interface implementing type is never needed. If you clearly expected your purpose, I would probably be able to explain how to avoid it on your example.

Second of all, you should not use ArrayList in new development. This type is rendered obsolete as early as of v.2.0, when generics were introduced. You should use strongly typed approach with the generic type System.Collections.Generic.List<> or other generic collection types. Please see:
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx[^],
http://msdn.microsoft.com/en-us/library/System.Collections.Generic.aspx[^].

This class wasn't formally marked obsolete only because there is nothing wrong with using it in legacy project which are otherwise backward-compatible with newer versions of the .NET framework. For new development it simply makes no sense, as not strongly typed collections require type case, which is an extra potential source of bugs.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900