Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用字典作为其他字典中的键_C#_Dictionary - Fatal编程技术网

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

C# 使用字典作为其他字典中的键,c#,dictionary,C#,Dictionary,我想用这本词典作为另一本词典的索引。类似于python的东西。我试过这个,但它给了我错误 Dictionary<Dictionary<string, string>, int> dict = new Dictionary<Dictionary<string, string>, int>(); Dictionary<string, string> dict2 = new Dictionary<string, string>

我想用这本词典作为另一本词典的索引。类似于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;

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

正确的答案是,你问的是:

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;

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

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

正确的答案是,你问的是:

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;

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

这会给您带来什么错误?它是不是在抱怨你在4号线上丢失的括号

第4行看起来应该是:

dict[dict["abc"]] = 20;
然而,你可能是这么想的,因为abc不是dict的关键:

dict[dict2["abc"]] = 20;
但是dict2[abc]是一个字符串,而dict的键应该是一个字典

class ComplexKey : Dictionary<String, String> { ... }
class ComplexType : Dictionary<ComplexKey, String> { ... }
但在这条路走得更远之前,让我们重新审视一下你最初的目标。首先,不应该将可变类型用作字典键

这可能是您要查找的代码:

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

但是很难说清楚。

它给了你什么错误?它是不是在抱怨你在4号线上丢失的括号

第4行看起来应该是:

dict[dict["abc"]] = 20;
然而,你可能是这么想的,因为abc不是dict的关键:

dict[dict2["abc"]] = 20;
但是dict2[abc]是一个字符串,而dict的键应该是一个字典

class ComplexKey : Dictionary<String, String> { ... }
class ComplexType : Dictionary<ComplexKey, String> { ... }
但在这条路走得更远之前,让我们重新审视一下你最初的目标。首先,不应该将可变类型用作字典键

这可能是您要查找的代码:

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

但这很难确定。

简单地说,我经常发现,处理你描述的复杂词典时,最好使用真名,而不是让代码的读者来整理

根据个人喜好,您可以选择两种方式之一。使用using语句创建编译器别名。注意:您必须使用完全限定类型名,因为这是编译器别名

using ComplexKey = System.Collections.Generic.Dictionary<String, String>;
using ComplexType = System.Collections.Generic.Dictionary<
   System.Collections.Generic.Dictionary<String, String>,
   String
   >;
或者,您可以使用完整的类型方式,实际创建一个从Dictionary继承的类

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

这样做将使您和代码的读者更容易了解您在做什么。我的一般经验法则是,如果我要创建一个泛型的泛型,那么是时候考虑构建一些一流的公民来代表我的逻辑了。

只是把它放在这里,我经常发现,处理你描述的复杂词典时,最好使用真名,而不是让代码的读者来整理

根据个人喜好,您可以选择两种方式之一。使用using语句创建编译器别名。注意:您必须使用完全限定类型名,因为这是编译器别名

using ComplexKey = System.Collections.Generic.Dictionary<String, String>;
using ComplexType = System.Collections.Generic.Dictionary<
   System.Collections.Generic.Dictionary<String, String>,
   String
   >;
或者,您可以使用完整的类型方式,实际创建一个从Dictionary继承的类

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

这样做将使您和代码的读者更容易了解您在做什么。我的一般经验法则是,如果我正在创建一个泛型的泛型,那么是时候考虑构建一些一等公民来代表我的逻辑了。

我自己也在键入同样的内容+1:好的,但是我如何从内部dict中获取特定的键、值对。我不想为外部dict中的每个值创建内部dict。dict[dict2[abc]=20对meI来说更有意义我只是自己键入了相同的内容+1:好的,但是我如何从内部dict中获取特定的键、值对。我不想为外部dict中的每个值创建内部dict。dict[dict2[abc]=20对meI更有意义。我将尝试此解决方案。谢谢我将尝试这个解决方案。谢谢您的最后一行有多个错误。当其中一个引用应该是dict2时,您引用dict两次。重要教训:清楚地命名变量!您有一个不匹配的大括号。您的最后一行有多个错误。当其中一个引用应该是dict2时,您引用dict两次。重要教训:清楚地命名变量!你有一个无与伦比的大括号。