In C# 3.0 you can create anonymous class with the following syntax
var o = new { Id = 1, Name = 'Foo' };
Is there a way to add these anonymous class to a generic list?
Example:
var o = new { Id = 1, Name = 'Foo' }; var o1 = new { Id = 2, Name = 'Bar' }; List<var> list = new List<var>(); list.Add(o); list.Add(o1);
Another Example:
List<var> list = new List<var>(); while (....) { .... list.Add(new {Id = x, Name = y}); .... }
You could do:
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:
You get the idea 🙂