Another option is a Hashtable of Hashtables, like this:
Hashtable mainTable = new Hashtable();
// Add myValue under key1, key2
Hashtable subTable = (Hashtable)mainTable[key1];
if (subTable == null)
{
subTable = new Hashtable();
mainTable[key1] = subTable;
}
subTable[key2] = myValue;
// Get myValue back, given key1 and key2
myValue = null;
Hashtable subTable = (Hashtable)mainTable[key1];
if (subTable != null)
{
myValue = (MyClass)subTable[key2];
}
// At this point, if myValue is null then it was not found; if it is
non-null then it was found.
Of course, you could create your own HashOfHashtables class that
wrapped all of this up in a nice package, including an indexer that
could take two keys and do the required lookup / assignment.
Nonetheless, this a more complicated implementation. You should use the
"concatenate the keys" method if possible, and use this only if you
have needs that the simpler solution doesn't meet (such as Larry's "get
me all the values with key1=='x'" operation).
By the way, if you use Larry's / Otis's "concatenate the keys" method,
be sure to add a delimeter in between the keys, something like this:
myHashTable.Add(key1 + "|" + key2, yourObject);
This is so that you don't run into the problem in which the keys "A"
and "BC" end up colliding with the key pair "AB" and "C". If you don't
use a delimeter (which should be outside the set of valid characters
for the key, if that's possible) then you could get collisions like
this. If you do use a delimeter then the first key would be, say,
"A|BC" and the second "AB|C" and there is no collision.