我需要在数组 Person 中添加数据。但是程序不是bug。 我不知道如何在构造函数中使用类型类构造函数。
public Person[] personData =
{
new Person {PersonId = 1, FirstName = "Adam", LastName ="Freeman",
HomeAddress = { City = "SoKa" , Country = "Look" } },
new Person {PersonId = 2, FirstName = "Nhan", LastName ="Ga",
HomeAddress = { City = "Dalat" , Country = "VungTau" } },
};
声明
public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Address HomeAddress { get; set; }
public class Address
{
public string City { get; set; }
public string Country { get; set; }
}
}
答案 0 :(得分:2)
你需要使用
HomeAddress = new Person.Address{ City = "SoKa" , Country = "Look" }
代替
HomeAddress = { City = "SoKa" , Country = "Look" }