Search code examples
c#genericsdictionary

Dictionary with two values per key?


I have a situation in code where a Dictionary<string, string> seemed like the best idea - I need a collection of these objects and I need them to be accessible via a unique key. Exactly what the Dictionary concept is for, right?

Well, the requirements have expanded to the point where I now need to hold an additional bit of information per-key (a boolean value, if you're curious).

So, I figure expand the concept to create a new data structure with the string and the boolean and have it now be a Dictionary<string, NewCustomObject>.

However, for just one additional value like a boolean flag, it just feels like overkill. And yet I don't know of any Dictionary-like generic object with two values per key.

Is just having a Dictionary of custom objects the best way to go about this or is there something simpler for this scenario?


Solution

  • Actually, what you've just described is an ideal use for the Dictionary collection. It's supposed to contain key:value pairs, regardless of the type of value. By making the value its own class, you'll be able to extend it easily in the future, should the need arise.