我有這2類:填充陣列直接通過對象初始化
class Customer
{
public string Name;
public string City;
public Order[] Orders;
}
class Order
{
public int Quantity;
public Product Product;
}
然後在Main
我做到以下幾點:
Customer cust = new Customer
{
Name = "some name",
City = "some city",
Orders = {
new Order { Quantity = 3, Product = productObj1 },
new Order { Quantity = 4, Product = productObj2 },
new Order { Quantity = 1, Product = producctObj3 }
}
};
但我不能初始化數組... with a collection initializer
。 我知道這一點,即可能string[] array = { "A" , "B" };
看起來相同,我...
當然我可以做的Order
單獨的對象,把它們放在一個數組,然後將其分配給Orders
,但我不不喜歡這個想法。
在這種情況下,我該如何實現清潔和代碼少的解決方案?
你沒有正確使用的初始化:使用Orders = new [] {}'來表示你正在初始化一個數組。 – 2014-09-28 14:38:32