Matthew Ruston
Matthew Ruston

Reputation: 4322

C# Collection Data Structure With 1:1 Key/Value Mapping

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?

Upvotes: 5

Views: 4694

Answers (2)

erickson
erickson

Reputation: 269847

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

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1503140

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.

Upvotes: 8

Related Questions