3

我可以从匿名类中定义数组或列表吗?

像这样:

persons = new ... [] 
{
 new { ID = 1, Name = "Name1"},
 new { ID = 2, Name = "Name2"}
}
4

1 回答 1

10

是的,您只需要persons隐式键入变量并从数组创建语句中删除类型说明符。

var persons = new []
{
    new { ID = 1, Name = "Name1" },
    new { ID = 2, Name = "Name2" }
}
于 2010-10-11T17:02:23.120 回答