A generic list of anonymous class

You could do:

var list = new[] { o, o1 }.ToList();

There are lots of ways of skinning this cat, but basically they’ll all use type inference somewhere – which means you’ve got to be calling a generic method (possibly as an extension method). Another example might be:

public static List<T> CreateList<T>(params T[] elements)
{
     return new List<T>(elements);
}

var list = CreateList(o, o1);

You get the idea 🙂

Leave a Comment