Re: Casting/converting generic list types
Jon Skeet <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.cx |
|---|---|
| Message-ID | <[email protected]> |
Mont Rothstein wrote: > I may or may not have a performance issue, it is a potential problem. I > was > primarily looking for a more elegant solution because looping over List<A> > to create List<B> is just ugly. > > Even better than a standard way to convert/cast would be the ability to > return a List<T> where T is either A or any subclass of A. I'm not really > sure why they didn't allow for this, perhaps it will get added in the > future. > > Thank you both for your suggestions and for confirming what I feared. Covariance and contravariance are supported for interface types at the IL level, but C# doesn't support them. As Andrew said, if you're happy with IList<T> instead of List<T>, you could write a wrapper. You'll lose the compile-time type safety you get with generics though. (If you wanted to return a List<B> as a List<A> you wouldn't have type-safety issues, but you'd still need to write a wrapper.) Jon =================================== This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com