var groups = new List<Group>
{
new Group{
Name = "Train",
Members = new List<Colleague>{
{FirstName = "Thomas", LastName = "Tank"},
{FirstName = "Honey", LastName = "Booboo"}
}
},
new Group{Name = "Bus"}
};
我在'FirstName'和'LastName'下得到一个红色下划线...意思是我错误地初始化了...
如何初始化该(同事)列表?我认为我错误地初始化该列表(成员)......
修改:为了让事情更清楚,
public class Group
{
public string Name { get; set; }
public List<Colleague> Members { get; set; }
}
编辑:跟随Kirk Woll进行讨论:
这就是我对你的建议所做的。成员是空的但是......
var groups = new List<Group>
{
new Group
{
Name = "Train",
Members = new List<Colleague>{
new Colleague { FirstName = "Thomas", LastName = "Tank" },
new Colleague { FirstName = "Jet", LastName = "Starr" }
}
},
new Group{Name = "Bus"}
};
总而言之,在群组初始化之后有一个断点。每个组(x2)都定义了Name,但在第一个Group('Train')中Members = null。会员必须初始化!干杯。
答案 0 :(得分:2)
你想:
Members = new List<Colleague>
{
new Colleague { FirstName = "Thomas", LastName = "Tank" },
new Colleague { FirstName = "Honey", LastName = "Booboo" }
}
因为它是您尝试添加的Colleague
列表。