2014-03-27 21 views
0

請我需要建立一個C#陣列以下確切格式:C#陣列特定的格式

var sampledata = { 
         {Day: "Sunday", Quantity: 15}, 
         {Day: "Monday", Quantity: 20}, 
         {Day: "Tuesday", Quantity: 80} 
        } 

我們可以創建在C#像上面。如果是,請如何? 感謝...

+0

看來你想製作一個對象數組,每個對象都有一個'Day'和'Quantity'屬性,這是否正確? – 2014-03-27 08:22:52

回答

2
var sampledata = new[] { 
    new { Day = "Sunday", Quantity = 15 }, 
    new { Day = "Monday", Quantity = 20 }, 
    new { Day = "Tuesday", Quantity = 80 } 
}; 

嘗試陣列或匿名類型(http://msdn.microsoft.com/en-US/en-en/library/bb397696.aspx

+1

一些額外的解釋,而不僅僅是一個代碼片段將改善這個答案。 – 2014-03-27 08:25:31

0

使用對象和數組在一起的列表?

class Order 
{ 
    public String Day; 
    public int Quantity; 
} 

Order[] orderArray = new Order[3]; 
orderArray[0].Date = "Sunday"; 
orderArray[0].Quantity = 15; 
...