C# Dictionary with two Values per Key?
Solution 1:
Actually, what you've just described is an ideal use for the Dictionary collection. It's supposed to contain key:value pairs, regardless of the type of value. By making the value its own class, you'll be able to extend it easily in the future, should the need arise.
Solution 2:
class MappedValue
{
public string SomeString { get; set; }
public bool SomeBool { get; set; }
}
Dictionary<string, MappedValue> myList = new Dictionary<string, MappedValue>;
Solution 3:
I think generally you're getting into the concept of Tuples - something like Tuple<x, y, z>
, or Tuple<string, bool, value>
.
C# 4.0 will have dynamic support for tuples, but other than that, you need to roll your own or download a Tuple library.
You can see my answer here where I put some sample code for a generic tuple class. Or I can just repost it here:
public class Tuple<T, T2, T3>
{
public Tuple(T first, T2 second, T3 third)
{
First = first;
Second = second;
Third = third;
}
public T First { get; set; }
public T2 Second { get; set; }
public T3 Third { get; set; }
}