C# 多值字典
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/569903/
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
Multi value Dictionary
提问by
How would i create a multi value Dictionary in c#?
我将如何在 c# 中创建一个多值字典?
E.g. Dictionary<T,T,T>
where the first T is the key and other two are values.
例如Dictionary<T,T,T>
,第一个 T 是键,其他两个是值。
so this would be possible: Dictionary<int,object,double>
所以这是可能的: Dictionary<int,object,double>
Thanks
谢谢
采纳答案by Jon Skeet
Just create a Pair<TFirst, TSecond>
type and use that as your value.
只需创建一个Pair<TFirst, TSecond>
类型并将其用作您的值。
I have an example of one in my C# in Depth source code. Reproduced here for simplicity:
我的C# in Depth 源代码中有一个示例。为简单起见,此处转载:
using System;
using System.Collections.Generic;
public sealed class Pair<TFirst, TSecond>
: IEquatable<Pair<TFirst, TSecond>>
{
private readonly TFirst first;
private readonly TSecond second;
public Pair(TFirst first, TSecond second)
{
this.first = first;
this.second = second;
}
public TFirst First
{
get { return first; }
}
public TSecond Second
{
get { return second; }
}
public bool Equals(Pair<TFirst, TSecond> other)
{
if (other == null)
{
return false;
}
return EqualityComparer<TFirst>.Default.Equals(this.First, other.First) &&
EqualityComparer<TSecond>.Default.Equals(this.Second, other.Second);
}
public override bool Equals(object o)
{
return Equals(o as Pair<TFirst, TSecond>);
}
public override int GetHashCode()
{
return EqualityComparer<TFirst>.Default.GetHashCode(first) * 37 +
EqualityComparer<TSecond>.Default.GetHashCode(second);
}
}
回答by Otávio Décio
I don't think you can do that directly. You could create a class containing both your object
and double
and put an instance of it in the dictionary though.
我不认为你可以直接做到这一点。您可以创建一个包含您object
和的类double
,并将它的一个实例放入字典中。
class Pair
{
object obj;
double dbl;
}
Dictionary<int, Pair> = new Dictionary<int, Pair>();
回答by teedyay
Dictionary<T1, Tuple<T2, T3>>
Edit: Sorry - I forgot you don't get Tuples until .NET 4.0 comes out. D'oh!
编辑:对不起 - 我忘了你在 .NET 4.0 出来之前不会得到元组。哦!
回答by Jason Punyon
If the values are related, why not encapsulate them in a class and just use the plain old Dictionary?
如果这些值是相关的,为什么不将它们封装在一个类中并只使用普通的旧字典?
回答by Eriawan Kusumawardhono
I think this is quite overkill for a dictionary semantics, since dictionary is by definition is a collection of keys and its respective values, just like the way we see a book of language dictionary that contains a word as the key and its descriptive meaning as the value.
我认为这对于字典语义来说是非常矫枉过正的,因为字典根据定义是键及其各自值的集合,就像我们看到一本语言字典的书一样,它包含一个单词作为键,它的描述性含义作为价值。
But you can represent a dictionary that can contain collection of values, for example:
但是您可以表示一个可以包含值集合的字典,例如:
Dictionary<String,List<Customer>>
Or a dictionary of a key and the value as a dictionary:
或者一个键和值的字典作为字典:
Dictionary<Customer,Dictionary<Order,OrderDetail>>
Then you'll have a dictionary that can have multiple values.
然后你就会有一个可以有多个值的字典。
回答by GurdeepS
You describe a multimap.
您描述了一个多图。
You can make the value a List object, to store more than one value (>2 for extensibility).
您可以将该值设为 List 对象,以存储多个值(>2 以实现可扩展性)。
Override the dictionary object.
覆盖字典对象。
回答by Jon Erickson
If you are trying to group values together this may be a great opportunity to create a simple struct or class and use that as the value in a dictionary.
如果您尝试将值组合在一起,这可能是创建简单结构或类并将其用作字典中的值的绝佳机会。
public struct MyValue
{
public object Value1;
public double Value2;
}
then you could have your dictionary
那么你可以拥有你的字典
var dict = new Dictionary<int, MyValue>();
you could even go a step further and implement your own dictionary class that will handle any special operations that you would need. for example if you wanted to have an Add method that accepted an int, object, and double
你甚至可以更进一步,实现你自己的字典类来处理你需要的任何特殊操作。例如,如果您想拥有一个接受 int、object 和 double 的 Add 方法
public class MyDictionary : Dictionary<int, MyValue>
{
public void Add(int key, object value1, double value2)
{
MyValue val;
val.Value1 = value1;
val.Value2 = value2;
this.Add(key, val);
}
}
then you could simply instantiate and add to the dictionary like so and you wouldn't have to worry about creating 'MyValue' structs:
那么你可以像这样简单地实例化并添加到字典中,你不必担心创建“MyValue”结构:
var dict = new MyDictionary();
dict.Add(1, new Object(), 2.22);
回答by Magnus Alexander
I solved Using:
我解决了使用:
Dictionary<short, string[]>
Like this
像这样
Dictionary<short, string[]> result = new Dictionary<short, string[]>();
result.Add(1,
new string[]
{
"FirstString",
"Second"
}
);
}
return result;
回答by Mr Heelis
I know this is an old thread, but - since it's not been mentioned this works
我知道这是一个旧线程,但是 - 由于没有提到它,所以它有效
Dictionary<string, object> LookUp = new Dictionary<string, object>();
LookUp.Add("bob", new { age = "23", height = "2.1m", weight = "110kg"});
LookUp.Add("jasper", new { age = "33", height = "1.75m", weight = "90kg"});
foreach(KeyValuePair<string, object> entry in LookUp )
{
object person = entry.Value;
Console.WriteLine("Person name:" + entry.Key + " Age: " + person.age);
}
回答by Colm Bhandal
Here is an implementation of a single key to multi value map in C# which uses a set based key type:
这是 C# 中单键到多值映射的实现,它使用基于集合的键类型:
https://github.com/ColmBhandal/CsharpExtras/blob/master/CsharpExtras/Dictionary/MultiValueMapImpl.cs
https://github.com/ColmBhandal/CsharpExtras/blob/master/CsharpExtras/Dictionary/MultiValueMapImpl.cs
The dictionary behaves like a regular dictionary from the key type onto a set of the value type, but also provides functionality to directly add a single value of the value type, and in the background handles the creation of an underlying set and/or addition to that set.
字典的行为类似于从键类型到值类型集合的常规字典,但也提供直接添加值类型的单个值的功能,并在后台处理底层集合的创建和/或添加到该集。