具有特定格式的C#数组

时间:2014-03-27 08:17:55

标签: c# arrays collections format

我需要创建一个C#数组,其格式如下:

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

我们可以在C#中创建类似上面的内容吗?如果有,请问怎么样? 感谢...

2 个答案:

答案 0 :(得分: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 :(得分:0)

一起使用对象和数组?

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

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