C#中多值字典的一个键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14987156/
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
One Key to multiple values dictionary in C#?
提问by Gisway
I often need one key to multiple vaules dictionary, but in C# most of them are two dimensions like Dictionary and Hashtable.
我经常需要一个键来访问多个 vaules 字典,但在 C# 中,它们中的大多数是两个维度,如 Dictionary 和 Hashtable。
I want something like this:
我想要这样的东西:
var d = new Dictionary<key-dt,value1-dt,value2-dt,value3-dt,value4-dt>();
dt inside<> means data type. Anybody has ideas about this?
dt inside<> 表示数据类型。有人对此有想法吗?
采纳答案by Aniket Inge
A dictionary is a key-value pair, where the value is fetched depending on the key. The keys are all unique.
字典是一个键值对,其中根据键获取值。钥匙都是独一无二的。
Now if you want a Dictionary
with 1 keytype and multiple value types, you have a few options:
现在,如果你想要一个Dictionary
具有 1 个键类型和多个值类型的,你有几个选择:
first is to use a Tuple
首先是使用 Tuple
var dict = new Dictionary<KeyType, Tuple<string, string, bool, int>>()
var dict = new Dictionary<KeyType, Tuple<string, string, bool, int>>()
The other is to use (with C# 4.0 and above):
另一种是使用(使用 C# 4.0 及更高版本):
var dict = new Dictionary<KeyType, dynamic>()
var dict = new Dictionary<KeyType, dynamic>()
the System.Dynamic.ExpandoObject
can have value of any type.
所述System.Dynamic.ExpandoObject
可具有任何类型的值。
using System;
using System.Linq;
using System.Collections.Generic;
public class Test {
public static void Main(string[] args) {
dynamic d1 = new System.Dynamic.ExpandoObject();
var dict = new Dictionary<int, dynamic>();
dict[1] = d1;
dict[1].FooBar = "Aniket";
Console.WriteLine(dict[1].FooBar);
dict[1].FooBar = new {s1="Hello", s2="World", s3=10};
Console.WriteLine(dict[1].FooBar.s1);
Console.WriteLine(dict[1].FooBar.s3);
}
}
回答by System Down
回答by Anthony Pegram
Describe both the appropriate key fields and the appropriate value fields with classes.and use a dictionary of those types.
用类描述适当的键字段和适当的值字段。并使用这些类型的字典。
var dictionary = new Dictionary<TheKeyType, TheValuesType>();
Note: If you have multiple values acting as the key, you would define a class to encapsulate those values and provide proper overrides of GetHashCode and Equals so that the dictionary could recognize their equality.
注意:如果您有多个值作为键,您将定义一个类来封装这些值并提供 GetHashCode 和 Equals 的适当覆盖,以便字典可以识别它们的相等性。
Short of doing this, you can utilize tuples, but you want to limit this pattern, as tuples are non self-describing.
如果不这样做,您可以使用元组,但您想限制这种模式,因为元组是非自描述的。
var dictionary = new Dictionary<Tuple<Key1Type, Key2Type, Etc>, Tuple<Value1Type, Value2Type, Etc>>();
回答by Luis Filipe
Instead of using a Tuple, which is a perfectly valid solution, i'd advise on creating your own class as the key and/or Value.
与其使用完全有效的解决方案元组,我建议您创建自己的类作为键和/或值。
You might realize the tuple will become an hard to read code.
您可能会意识到元组将成为难以阅读的代码。