[Mono-list] Generics Conversions

Ricky Clarkson ricky.clarkson at gmail.com
Mon Feb 23 08:36:29 EST 2009


Alternative implementation: return e.Select(x => (U)x);

2009/2/23 Chris Howie <cdhowie at gmail.com>

> On Mon, Feb 23, 2009 at 8:10 AM, Ricky Clarkson
> <ricky.clarkson at gmail.com> wrote:
> > Right now I'd use IEnumerable<object> objects = stringList.Select(x =>
> > (object)x); or if you must have List<>, List<object> objects =
> > stringList.Select(x => (object)x).ToList();
> >
> > The advantage of the IEnumerable is that no actual copying is done.
>
> Maybe something like this could be used until C# 4 hits:
>
> public static IEnumerable<U> Upcast<T, U>(this IEnumerable<T> e) where T :
> U {
>    foreach (T i in e)
>        yield return i;
> }
>
> IEnumerable<object> e = someStringList.Upcast<string, object>();
>
> --
> Chris Howie
> http://www.chrishowie.com
> http://en.wikipedia.org/wiki/User:Crazycomputers
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20090223/b23f2ba5/attachment.html 


More information about the Mono-list mailing list