如何在自定义类中创建名称数组
本文关键字:创建 数组 自定义 | 更新日期: 2023-09-27 18:21:24
所以我正在C#中创建一个新工具,我创建了一个名为"Customer"的类,每个"Customer"都有一个子组员工,这是一个名称数组。如何在类中为"Customer"设置此属性?我下面关于"员工"的内容不正确。我只是把它作为占位符放在那里。谢谢
public class Customer
{
public String Number { get; set; }
public String Name { get; set; }
public String Street { get; set; }
public String City { get; set; }
public String State { get; set; }
public String Zipcode { get; set; }
public string[] Employees = { get; set; }
}
您可以使用List
而不是数组,因为它更容易操作:
public class Customer
{
public String Number { get; set; }
public String Name { get; set; }
public String Street { get; set; }
public String City { get; set; }
public String State { get; set; }
public String Zipcode { get; set; }
public List<string> Employees { get; set; }
}
然后,当你实例化它时,你可以添加这样的新雇主:
Customer customer = new Cusomter();
customer.Number = "num1";
customer.Name = "ABC";
//...
List<string> lstEmp = new List<string>();
lstEmp.Add("NewEmployee1");
lstEmp.Add("NewEmployee2");
customer.Employees = lstEmp;
这样读:
foreach (string name in customer.Employees)
{
Console.WriteLine(name);
}
只需使用以下声明:
public string[] Employees { get; set; }