C#在实例化时分配给字典

时间:2015-06-24 19:42:03

标签: c# .net

在实例化时分配给字典的最佳方法是什么?

例如,我如何才能myDictionary低于我myList所做的一切?

Dictionary<int, string> myDictionary = new Dictionary<int, string>()
{
    // this does not work. how to make it work?
    (0, "first"),
    (1, "second")
};

// here's how it's done with List<T>
List<Int32> myList = new List<int>(){
    1,
    2,
    3
};

1 个答案:

答案 0 :(得分:4)

Dictionary<int, string> myDictionary = new Dictionary<int, string>()
{
    {0, "first"},
    {1, "second"}
};