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,232
  • 7
  • 35
  • 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,335,956
  • 823
  • 8,931
  • 9,049
3

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

Janus Troelsen
  • 19,210
  • 14
  • 126
  • 189
erickson
  • 257,800
  • 54
  • 385
  • 479