【问题标题】:How to constructor in constructor [closed]如何在构造函数中构造函数[关闭]
【发布时间】:2021-01-31 17:46:53
【问题描述】:

我需要在数组 Person 中添加数据。但程序不是错误。 我不知道如何在构造函数中使用类型类进行构造函数。

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; }
    }
}

【问题讨论】:

    标签: c# class constructor


    【解决方案1】:

    你需要使用

    HomeAddress = new Person.Address{ City = "SoKa" , Country = "Look" }
    

    而不是

    HomeAddress = { City = "SoKa" , Country = "Look" }
    

    【讨论】:

      猜你喜欢
      • 2013-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多