Multi-valued dictionary

How to create a multi-valued dictionary in C #?

eg. Dictionary<T,T,T> , where the first T is the key, and the other two are the values.

to make this possible: Dictionary<int,object,double>

thank

+55
c #
Feb 20 '09 at 15:14
source share
8 answers

Just create the type Pair<TFirst, TSecond> and use this as your value.

I have an example of one of C # in the depth source code . Reproduced here for simplicity:

 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); } } 
+57
Feb 20 '09 at 15:18
source share

If you are trying to group values ​​together, this can be a great opportunity to create a simple structure or class and use it as a value in a dictionary.

 public struct MyValue { public object Value1; public double Value2; } 

then you can have your dictionary

 var dict = new Dictionary<int, MyValue>(); 

you can even go further and implement your own dictionary class, which will handle any special operations that you need. for example, if you want to have an Add method that accepts int, object and double

 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 can just create an instance and add it to the dictionary like this, and you don’t have to worry about creating MyValue structures:

 var dict = new MyDictionary(); dict.Add(1, new Object(), 2.22); 
+41
Feb 20 '09 at 16:31
source share
 Dictionary<T1, Tuple<T2, T3>> 

Edit: Sorry - I forgot that you are not getting Tuples until .NET 4.0 comes out. D'o!

+19
Feb 20 '09 at 15:18
source share

I think this overwhelms the semantics of the dictionary, because the dictionary by definition is a set of keys and its corresponding meanings, just like we see a dictionary book containing a word as a key and its descriptive meaning as meaning.

But you can imagine a dictionary that can contain a set of values, for example:

 Dictionary<String,List<Customer>> 

Or a key dictionary and a value like a dictionary:

 Dictionary<Customer,Dictionary<Order,OrderDetail>> 

Then you will have a dictionary that can have several meanings.

+5
Feb 20 '09 at 15:25
source share

I do not think you can do this directly. You can create a class containing both object and double , and place it in the dictionary.

 class Pair { object obj; double dbl; } Dictionary<int, Pair> = new Dictionary<int, Pair>(); 
+3
Feb 20 '09 at 15:18
source share

If the values ​​are related to each other, why not encapsulate them in a class and just use a simple old dictionary?

+2
Feb 20 '09 at 15:18
source share

You are describing multimap.

You can make a value a List object to store more than one value (> 2 for extensibility).

Move the dictionary object.

+1
Feb 20 '09 at 15:30
source share

I decided to use:

 Dictionary<short, string[]> 

Like this

 Dictionary<short, string[]> result = new Dictionary<short, string[]>(); result.Add(1, new string[] { "FirstString", "Second" } ); } return result; 
0
Jan 12 '17 at 18:04 on
source share



All Articles