wolfgang ziegler


„make stuff and blog about it“

List of Anonymous Types in C'#

July 18, 2012

In an ASP.NET MVC application I recently had to create a list holding very simple data objects for populating the UI. I was wondering, whether I could do this without creating an actual class for this data objects and just creating a list of anonymous types. It turned out to be quite simple and boils down to these few lines of code:

var list = new []
{ 
  new { FirstName = "Wolfgang", LastName = "Ziegler" },
  new { FirstName = "Bender", LastName = "Rodriguez" },
  new { FirstName = "Dexter", LastName = "Morgan" },
  new { FirstName = "Walter", LastName = "White" },
}.ToList();