需要帮助c#编码

Tho*_*mas 0 c#

这是我的班级结构

public class Customer
{
     List<Address> add = null;
     public Customer()
     {
         add = new List<Address>();
     }

    public int EmpID { get; set; }
    public string Name { get; set; }

    public List<Address> Address
    {
        get { return add; }
        set { add = value; }
    }
    public double Salary { get; set; }
}

public class Address
{
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string PostCode { get; set; }
}


    public void Populate()
    {
        List<Customer> oCust = new List<Customer>()
        {
                new Customer() { EmpID=1, Name="Sonia"},
                new Customer() { EmpID=2, Name="Bill"},
                new Customer() { EmpID=3, Name="Mark"},
        };
    }
Run Code Online (Sandbox Code Playgroud)

从populate方法我填充客户和地址.当我填充客户,然后如何填充地址,我不是一个高级用户,所以请指导我如何代码,如何 new Customer() { EmpID=3, Name="Mark" new Address{}},

请帮忙....谢谢

Dar*_*rov 11

List<Customer> oCust = new List<Customer>()
{
    new Customer() 
    { 
        EmpID = 1, 
        Name = "Sonia", 
        Address = 
        { 
            new Address { Address1 = "foo", Address2 = "bar", PostCode = "pc" },
            new Address { Address1 = "foo 2", Address2 = "bar 2", PostCode = "pc 2" },
        } 
    },
    new Customer() 
    { 
        EmpID = 2, 
        Name = "Bill", 
        Address = 
        { 
            new Address { Address1 = "bill 1", Address2 = "bill 2", PostCode = "bill pc" },
        } 
    }
};
Run Code Online (Sandbox Code Playgroud)

  • 你不需要`new List <Address>`,因为它是在构造函数中创建的...... (2认同)