The traditional dictionary will hold key-value
pairs. I need something to hold key-key
pairs where key1
and key2
will be two different types of objects. For instance,
SourceIdentity key1 = dict[key2]; //key2 index gives key1 value
TargetIdentity key2 = dict[key1]; //key1 index gives key2 value
In other words both the keys should yield the other value, kind of a two way access. I would be adding the values just like how I otherwise would:
dict.Add(s, t);
And no there won't be any duplicates in key1s and key2s since they are keys.
This is what I can think of, but I think there can be a lighter way:
public class FunnyDictionary
{
var key1Dict = new Dictionary<SourceIdentity, TargetIdentity>();
var key2Dict = new Dictionary<TargetIdentity, SourceIdentity>();
public void Add(SourceIdentity key1, TargetIdentity key2)
{
key1Dict[key1] = key2;
key2Dict[key2] = key1;
}
//remaining part..
}
I have seen this question Multi-key dictionary in c#? but nothing like my situation now. I want a dictionary so that I can get quick access to values otherwise which I will have to enumerate a collection every time which I feel can be avoided. The collection will have more than 10000 entries. Is this the lightest I can have? Something simpler or better in any other way?