2014-09-11 45 views
1

因此,我在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

回答

3

你可以使用一個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

1

只需使用此聲明:

public string[] Employees { get; set; } 
相關問題