[Mono-list] Generics Conversions

Chris Howie cdhowie at gmail.com
Mon Feb 23 08:31:17 EST 2009


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


More information about the Mono-list mailing list