如何在c#中添加多个匿名类型?

时间:2012-01-25 09:34:19

标签: c#-3.0

考虑一下

var source = new{  Id = "1", Name = "Name1"}

工作正常。但是,如果我想再增加一个属性怎么做......

失败

var source = new{  Id = "1", Name = "Name1"},new{  Id = "1", Name = "Name1"}

这是什么语法?

1 个答案:

答案 0 :(得分:7)

在MSDN上查看Collection initializersanonymous types

var source = new [] { new {  Id = "1", Name = "Name1"},
                      new {  Id = "2", Name = "Name2"}};