如何在自定义类中创建名称阵列

问题描述:

因此,我在C#中创建了一个新工具,并且创建了一个名为“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; } 
} 
+0

你为什么不使用'List'呢? '列表' – Prix 2014-09-11 04:29:46

+0

我如何在代码中正确写入? – JokerMartini 2014-09-11 04:30:14

+0

'public string [] Employees {get;组; }'? – 2014-09-11 04:31:16

你可以使用一个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); 
} 
+0

'List '是更好的选择:) – Hassan 2014-09-11 04:38:49

+0

我打算使用这个List方法,因为根据你们所说的一切,这是更好的方法。再次感谢你们 – JokerMartini 2014-09-11 05:37:18

只需使用此声明:

public string[] Employees { get; set; }