使用字符串查找C#字典

Chr*_*son 2 c# dictionary

我想通过使用字符串找到一个字典,以便将其复制到一个新的字典中 - 看起来它应该很简单,但我不确定语法.

基本上它只是:

    // the object's myName corresponds to an existing dictionary
    string stringdic = myName;      

    // here's where I try to create a new dictionary containing the values of the existing dictionary
    Dictionary<string, int> mySkills = new Dictionary<string, int>(myName);
Run Code Online (Sandbox Code Playgroud)

这里的目标只是创建一个具有给定字符串作为其"名称"的对象,并从名称知道它应该使用的一组字典中的哪一个 - 例如,如果myName = Bob,我想访问字典Bob在这个脚本中.

因此,引用预先存在的字典以获得其键/值的一些其他方式也将起作用.速度不是一个大问题,但我想保持代码简单.谢谢!

Jer*_*son 5

这是你如何做词典词典:

var persons = new Dictionary<string, Dictionary<string, int>>
{
    { "Jeremy", new Dictionary<string, int> { { "age", 20 }, { "height_cm", 180 } } },
    { "Evan", new Dictionary<string, int> { { "age", 18 }, { "height_cm", 167 } } },
};

Dictionary<string, int> x = persons["Jeremy"];
x["age"] = 34;
Run Code Online (Sandbox Code Playgroud)