49

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?

Tom Kidd
  • 12,830
  • 19
  • 89
  • 128
  • 18
    Your first idea of Dictionary is not an overkill at all. It expresses the exact intent, is strongly-typed, intellisense-enabled, will easily accommodate the next change in requirements. – Oren Trutner Sep 30 '09 at 21:53
  • 2
    @Oren Trutner: Cannot agree more. @Schnapple: Don't bother with a Pair, use a custom object, with well-defined names. Avoid, for example, "Value1" and "Value2" for the field names. Give them semantic meaning. – Randolpho Sep 30 '09 at 22:00
  • 1
    There is nothing that approaches 'overkill' when creating new classes in an OO language. Well, usually there is not, and in this case there is not. – Ed S. Oct 01 '09 at 00:28
  • possible duplicate of [Multi value Dictionary](http://stackoverflow.com/questions/569903/multi-value-dictionary) – nawfal Mar 30 '13 at 20:18
  • I know it's an old question, but in modern C# you can use [Named Tuples](https://www.google.com/search?q=c%23+7+named+tuples&oq=c%23+7+named+tuples) as well. – Hamid Mayeli Jul 31 '19 at 08:14
  • The [Multivalue](https://stackoverflow.com/questions/569903/multi-value-dictionary) dictionary post might serve your needs. – Anax Sep 30 '09 at 21:51

10 Answers10

38

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.

Traveling Tech Guy
  • 27,194
  • 23
  • 111
  • 159
  • I wound up going this route as the others looked equally as complicated and it's in .NET 3.5 so no Tuples for me – Tom Kidd Sep 30 '09 at 22:41
20
class MappedValue
{
    public string SomeString { get; set; }
    public bool SomeBool { get; set; }
}

Dictionary<string, MappedValue> myList = new Dictionary<string, MappedValue>;
Ed S.
  • 122,712
  • 22
  • 185
  • 265
9

I think generally you're getting into the concept of Tuples - something like Tuple<x, y, z>, or Tuple<string, bool, value>.

C# 4.0 will have dynamic support for tuples, but other than that, you need to roll your own or download a Tuple library.

You can see my answer here where I put some sample code for a generic tuple class. Or I can just repost it here:

public class Tuple<T, T2, T3>
{
    public Tuple(T first, T2 second, T3 third)

    {
        First = first;
        Second = second;
        Third = third;
    }

    public T First { get; set; }
    public T2 Second { get; set; }
    public T3 Third { get; set; }

}
Community
  • 1
  • 1
womp
  • 115,835
  • 26
  • 236
  • 269
7

In .NET4, you could use (unchecked): Dictionary<string, Tuple<bool,string>>

H H
  • 263,252
  • 30
  • 330
  • 514
3

I don't believe .net has Multi-Map built in which is generally a data-structure you can use to do this type of storage. On the other hand I don't think it's overkill at all just using a custom object that holds both a string and a boolean.

Ralph Caraveo
  • 10,025
  • 7
  • 40
  • 52
  • Multi-maps let you store multiple values against a given key, but the values are of the same type and are not otherwise distinguished, so that would not fit this need. – Daniel Earwicker Sep 30 '09 at 22:24
1

Doesn't this work for ya?

Dictionary<string, List<string>>

Or you could use a Tuple and have a dictionary of that:

Dictionary<string, Tuple<string, bool>>
Community
  • 1
  • 1
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
0

A Dictionary is just a keyed collection of objects. As such, it will hold any type of object you want. Even the simplest of objects, like, say, an array (which derives from Object), but requires a lot less code on your behalf.

Rather than writing an entire class, stuff a dynamically allocated array, or a List in the value and run with that.

Dictionaries are highly flexible that way.

Mike Hofer
  • 16,477
  • 11
  • 74
  • 110
0

What about Lookup class?

Edit:

After read the question carefully think this is not the best solution, but it still a good one for the dictionary with some values per a single key.

Kamarey
  • 10,832
  • 7
  • 57
  • 70
0

Another idea is to store the boolean in a separate data structure, e.g. a HashSet.

reinierpost
  • 8,425
  • 1
  • 38
  • 70
0

Tuple is always a good solution. Furthermore in an object oriented approach, always favour composition over inheritance. Construct a composite object doing the grouping. Simply. I think you are covered with some nice and clean solutions here, from fellow stackoverflow'ers. :)

Aggelos Biboudis
  • 1,175
  • 8
  • 27