C# 使用字典作为其他字典的键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/433372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:12:39  来源:igfitidea点击:

using dictionary as a key in other dictionary

c#dictionary

提问by kamilwydrzycki

I would like to use Dictionaryas TKeyin another Dictionary. Something similar to python. I tried this but it gives me errors.

我想用DictionaryTKey另一种Dictionary。类似于python的东西。我试过这个,但它给了我错误。

Dictionary<Dictionary<string, string>, int> dict = new Dictionary<Dictionary<string,    string>, int>();
Dictionary<string, string> dict2 = new Dictionary<string, string>();
dict2["abc"] = "def";
dict[dict["abc"] = 20;

采纳答案by recursive

What error is it giving you? Is it complaining about your missing bracket on line 4?

它给你什么错误?它是在抱怨第 4 行缺少括号吗?

Line 4 looks like it should be:

第 4 行看起来应该是:

dict[dict["abc"]] = 20;

However, you probably mean this, since "abc" is not a key of dict:

但是,您可能是这个意思,因为“abc”不是 dict 的键:

dict[dict2["abc"]] = 20;

But dict2["abc"]is a string, when the key of dict is supposed to be a Dictionary<string, string>.

但是dict2["abc"]是 a string,当 dict 的键应该是 a 时Dictionary<string, string>

But let's re-examine your original goal at this point before going to far down this path. You shouldn't be using mutable types as dictionary keys in the first place.

但是,在深入这条道路之前,让我们重新审视一下您此时的原始目标。首先,您不应该使用可变类型作为字典键。

This may be the code you're looking for:

这可能是您正在寻找的代码:

Dictionary<string, int> dict  = new Dictionary<string, int>();
Dictionary<string, string> dict2 = new Dictionary<string, string>();
dict2["abc"] = "def";
dict[dict2["abc"]] = 20;

But it's hard to tell for sure.

但很难确定。

回答by TcKs

It's because the "dict["abc"] is not dictionary, but "string".

这是因为“dict[”abc”] 不是字典,而是“字符串”。

The correct, what you asked is:

正确的,你问的是:

Dictionary<Dictionary<string, string>, int> dict = new Dictionary<Dictionary<string,    string>, int>();
Dictionary<string, string> dict2 = new Dictionary<string, string>();
dict2["abc"] = "def";
dict[dict2] = 20;

But i'm not sure, this is what you realy want/need.

但我不确定,这就是你真正想要/需要的。

回答by Peter Oehlert

Just to throw this in there, I often find that dealing with complicated dictionaries like you describe it's far better to use real names with them rather than trying to let the reader of the code sort it out.

只是为了把它放在那里,我经常发现处理像你描述的复杂字典最好使用真实姓名而不是试图让代码的读者把它整理出来。

You can do this one of two ways depending on personal preference. Either with a using statement to create a complier alias. Note you have to use the fully qualified type name since this is a compiler alias.

您可以根据个人喜好使用以下两种方式之一进行此操作。要么使用 using 语句创建编译器别名。请注意,您必须使用完全限定的类型名称,因为这是编译器别名。

using ComplexKey = System.Collections.Generic.Dictionary<String, String>;
using ComplexType = System.Collections.Generic.Dictionary<
   System.Collections.Generic.Dictionary<String, String>,
   String
   >;

Or you can go the full blown type way and actually create a class that inherits from Dictionary.

或者,您可以采用完整的类型方式并实际创建一个继承自 Dictionary 的类。

class ComplexKey : Dictionary<String, String> { ... }
class ComplexType : Dictionary<ComplexKey, String> { ... }

Doing this will make it far easier for both you and the reader of your code to figure out what you're doing. My general rule of thumb is if I'm creating a generic of a generic it's time to look at building some first class citizens to represent my logic rather.

这样做会让你和你的代码的读者更容易弄清楚你在做什么。我的一般经验法则是,如果我正在创建泛型的泛型,那么是时候考虑构建一些一流的公民来代表我的逻辑了。