如何将字典导入2-d表od对象?

时间:2017-06-29 14:25:40

标签: c# .net

我有这样的代码:

Dictionary<string, string> dict;
.
.
some[2] = new object[,]
{
{"key1", "value1"},
{"key2", "Value2"},
{"key3", "Value3"},
}; 

如何自动将dict中的键和值插入某些[2]?谢谢!

2 个答案:

答案 0 :(得分:0)

您可以随时循环执行此操作,例如:

Dictionary<string, string> dict = new Dictionary<string, string>()
{
    { "1", "A" },
    { "2", "B" },
    { "3", "C" },
    { "4", "D" }
};

var some = new string[dict.Keys.Count, 2];

int i = 0;
foreach(var item in dict)
{
    some[i, 0] = item.Key;
    some[i, 1] = item.Value;
    i++;
}

结果:

1 A
2 B
3 C
4 D

答案 1 :(得分:0)

您可以通过foreach语句遍历整个字典:

foreach (var dictItem in dict)
{
    var key = dictItem.Key;
    var value = dictItem.Value;
    // your code to add them somewhere.
}