5

Are there any built-in C# data structures that are like a hash table but requires both the Keys and the Values to be unique among each other? I basically want a way to look up my Key object in a table via a unique Value and vice-versa. Next to maintaining two hash tables or iterating over each key in the hash table (which is slow), I cannot think of a better way to implement this.

Is the fact that I even have this request a sign that I am designing something incorrectly in my code?

Matthew Ruston
  • 4,282
  • 7
  • 38
  • 47

2 Answers2

8

It's not necessarily a design flaw, but there's nothing built into the framework. Fortunately, there was a very similar question recently, and I wrote an answer which should serve you well.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

Google Collections calls this a BiMap. Maybe you could port this to C#?

Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
erickson
  • 265,237
  • 58
  • 395
  • 493