访问字典中的数组

时间:2014-11-13 15:11:15

标签: c# arrays dictionary char int

您好,我有这段代码

Dictionary<int[], char[]> items = new Dictionary<int[], char[]>();

我希望能够获得int []和char []的所有值,我已经尝试了

foreach (KeyValuePair<int[], char[]> kvp in items)
            {
                Console.WriteLine("Key = {0}, Value = {1}",
                    kvp.Key, kvp.Value);
            }

但这不起作用只是输出

Key = System32.Int32[], Value = System.Char[]
Key = System32.Int32[], Value = System.Char[]
Key = System32.Int32[], Value = System.Char[]
Key = System32.Int32[], Value = System.Char[]
Key = System32.Int32[], Value = System.Char[]

两个数组的长度相同,在本例中为5,所以我只是想知道如何访问字典中的某些元素?正常的方法不起作用。任何帮助是极大的赞赏。谢谢。

1 个答案:

答案 0 :(得分:2)

Console.WriteLine对参数调用ToString,您所看到的是在不覆盖ToString的类型上调用ToString方法的行为。 ToString的实现返回类型名称。如果要以其他格式显示值,则需要手动执行。

例如,如果要以逗号分隔格式显示数组值,可以使用string.Join

foreach (KeyValuePair<int[], char[]> kvp in items)
{
    Console.WriteLine("Key = {0}, Value = {1}",
                string.Join("," kvp.Key), string.Join("," kvp.Value));
}
相关问题