0

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?

Community
  • 1
  • 1
nawfal
  • 70,104
  • 56
  • 326
  • 368
  • Thanks @Jon I couldn't find it.. – nawfal Mar 28 '13 at 11:17
  • The point of a `Dictionary` is that you save both `Key` and `Value` into an `Array` at the `Index` defined by the Key's `Hash`, so you can tell right away, where the Value is stored, by looking at the Key's Hash. The only way I can think of, that would do what you're looking for, would need a system similar to how RSA encrypts and decrypts data. – Nolonar Mar 28 '13 at 11:18

0 Answers0