How to "cast" from generic List<> to ArrayList

Posted by Michael Freidgeim on Geeks with Blogs See other posts from Geeks with Blogs or by Michael Freidgeim
Published on Sun, 06 Jun 2010 02:10:34 GMT Indexed on 2010/06/06 2:23 UTC
Read the original article Hit count: 306

Filed under:

We are writing new code using generic List<> , e.g. List<MyClass>.
 
However we have legacy functions, that are expect ArrayList as a parameter.
It is a second time, when I and my colleague asked, how to "cast" generic List<MyClass> to ArrayList.
The answer is simple- just use ArrayList constructor with ICollection parameter.
Note that it is not real cast, it  copies  references to ArrayList.


var list=new List<MyClass>();

//Fill list items

ArrayList al=new ArrayList(list);//"cast"- 
 

 


 

© Geeks with Blogs or respective owner