dictionary (hash table) with two values?

T

titan.nyquist

Is there a typical way to create a dictionary (or hash table) with two
values, instead of one?

Currently, my data structure is TWO dictionaries, each with matching
and fully sychronized keys. This allows me to have one key with two
values (one value in each dictionary). This is ugly code as the
dictionaries could get out of synch (due to a bug or something).

Titan
 
J

Jon Skeet [C# MVP]

Is there a typical way to create a dictionary (or hash table) with two
values, instead of one?

Currently, my data structure is TWO dictionaries, each with matching
and fully sychronized keys. This allows me to have one key with two
values (one value in each dictionary). This is ugly code as the
dictionaries could get out of synch (due to a bug or something).

Just create a Pair type which has a pair of values, and store that as
the single value in the dictionary.
 
M

Mythran

Jon Skeet said:
Just create a Pair type which has a pair of values, and store that as
the single value in the dictionary.

Along these lines, you could also just store the values as an array...so
each key/value would be a key/array.

HTH,
Mythran
 
N

Nicholas Paldino [.NET/C# MVP]

Mythran,

The only catch there is that if the two values are of different types,
then you have to have an array of object, which can have an overhead (if you
are boxing and unboxing value types).

In all cases, the solution that Jon suggested will provide a consistent,
expected behavior.
 
P

PS

Is there a typical way to create a dictionary (or hash table) with two
values, instead of one?

Currently, my data structure is TWO dictionaries, each with matching
and fully sychronized keys. This allows me to have one key with two
values (one value in each dictionary). This is ugly code as the
dictionaries could get out of synch (due to a bug or something).

Titan

As Jon suggested use an object with two properties. Here is a generic
version that you would use like:

DualValueDictionary<long, string, int> d = new DualValueDictionary<long,
string, int>();
d.Add(123, "hello", 25);
if(d[123].Value1 == "hello") .....


public class DualValueDictionary<Tkey, V1, V2> : Dictionary<Tkey,
DualValues<V1, V2>>
{
public void Add(Tkey key, V1 value1, V2 value2)
{
base.Add(key, new DualValues<V1,V2>(value1, value2));
}
}
public class DualValues<V1, V2>
{
public DualValues(V1 v1, V2 v2)
{
this.value1 = v1;
this.value2 = v2;
}
private V1 value1;
public V1 Value1
{
get
{
return this.value1;
}
}
private V2 value2;
public V2 Value2
{
get
{
return this.value2;
}
}
}

PS
 
T

titan.nyquist

Jon, simple elegant solution. Thanks.

PS, thanks for the code. You just guided me around a few pitfalls I
would have got stuck on (I would not have thought of generic classes,
as I'm new to them).

Thanks all,
Titan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top