我可以从匿名类中定义一个数组或列表吗?
像这样:
persons = new ... []
{
new { ID = 1, Name = "Name1"},
new { ID = 2, Name = "Name2"}
}
我可以从匿名类中定义一个数组或列表吗?
像这样:
persons = new ... []
{
new { ID = 1, Name = "Name1"},
new { ID = 2, Name = "Name2"}
}
是的,你只需要隐式地输入persons
变量,并从数组创建语句中移除类型说明符。
var persons = new []
{
new { ID = 1, Name = "Name1" },
new { ID = 2, Name = "Name2" }
}
ToList()
,然后persons
将成为一个List<>
。 - Adam Robinson