views:

2968

answers:

10

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?

A: 

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>>
Mehrdad Afshari
So how does he store the boolean with this?
Ralph
Or equivalently, `System.Collections.Specialized.NameValueCollection`.
Steve Guidi
Ralph: I answered the title initially. Added a solution for that case.
Mehrdad Afshari
Steve: `NameValueCollection` is not a hash table. It performs `O(n)` searches.
Mehrdad Afshari
+3  A: 

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
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
+8  A: 

The Multivalue dictionary post might serve your needs.

Anax
Might be worth it just to have Jon Skeet code in my app :)
Schnapple
Jon Skeet's example will suit this need perfectly and it's generic!
Ralph
+3  A: 

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; }

}
womp
+6  A: 

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
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
Schnapple
Wise decision :)
Traveling Tech Guy
A: 

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
A: 

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
+5  A: 
class MappedValue
{
    public string SomeString { get; set; }
    public bool SomeBool { get; set; }
}

Dictionary<string, MappedValue> myList = new Dictionary<string, MappedValue>;
Ed Swangren
A: 

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

reinierpost
+1  A: 

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 Mpimpoudis