Declaration of Anonymous types List

Krishna picture Krishna · Feb 24, 2010 · Viewed 98.3k times · Source

Is there any way to declare a list object of anonymous type. I mean

List<var> someVariable = new List<var>();
someVariable.Add(
             new{Name="Krishna", 
                 Phones = new[] {"555-555-5555", "666-666-6666"}}
                );

This is because I need to create a collection at runtime.

Answer

Jai Kannah picture Jai Kannah · Mar 29, 2012

How about dynamic?

List<dynamic> dynamicList = new List<dynamic>();


dynamicList.Add(new { Name = "Krishna",  Phones = new[] { "555-555-5555", "666-666-6666" } });