Casting/converting generic list types
Mont Rothstein <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.cx |
|---|---|
| Message-ID | <[email protected]> |
If I have List<A> where all of the elements are of type B. How do I get List<B> from List<A>? Do I have to iterate over List<A> and add each element to List<B>? I've tried using "as List<B>" and "(List<B>)" both to no avail. List<T>.AddRange() must be of the same type T. ConvertAll() is an O(n) operation, and does a conversion which I don't really need. I have this situation occur fairly frequently because A is an abstract superclass of a number of different classes and it has a method to return a List of objects of a subclass, but that list is always homogeneous. Insight appreciated, -Mont =================================== This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com