如何打印这本词典

本文关键字:打印 何打印 | 更新日期: 2023-09-27 18:28:17

所以我有一个有点复杂的字典:

Dictionary<string, Dictionary< string, HashSet< string >>>

如何打印其键和值?

如何打印这本词典

//example
using System;
using System.Collections.Generic;
class Program
{
    static void Main()
    {
        var dic = new Dictionary<string, Dictionary<string,HashSet<string>>>
        {
            {"k", new Dictionary<string,HashSet<string>>
                {
                    {"k1", new HashSet<string>{"a","b","c"}}
                }
            },
            {"k3", new Dictionary<string,HashSet<string>>
                {
                    {"k4", new HashSet<string>{"1","2","3"}}
                }
            }
        };
        foreach(var p in dic)
        {
            Console.Write(p.Key + " -- ");
            foreach(var p1 in p.Value)
            {
                Console.Write(p1.Key + " -- ");
                foreach(var str in p1.Value)
                {
                    Console.Write(str + " ");
                }
            }
            Console.WriteLine();
        }
    } 
}
output: k -- k1 -- a b c
        k3 -- k4 -- 1 2 3 

要将所有值放入字符串中,可以使用以下

Dictionary<string,string> dictionary;
//[...]
string output = "";
foreach(string key in dictionary.Keys)
{
    output += "'nKey: '""+key+"'" Value: '""+dictionary[key]+"'"";
}
MessageBox.Show(output);

我想你会想调整格式,但这应该会有所帮助。