views:

745

answers:

5

I want to store an list of key value pair lists in a lightweight structure. This seems too cumbersome. What's better? Does List<Dictionary<string, string>> add much a overhead? What other options are available?

+2  A: 

Dictionary< string, string> and List< KeyValuePair< string, string>> could both be fine, depending on what data you wanted to pass around. Also, if you are going to use the same long type all over the place you could define the type somewhere else for a shorthand. Something like this:

public class MyShorthand : List<List<KeyValuePair<string, string>>> { }

Or you can use a using statement to define a type alias like this:

using MyShorthand = System.Collections.Generic.List<System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, string>>>;
Jake Pearson
The recommended way to alias types is the following: `using MyShorthand = List<List<KeyValuePair<string, string>>>;`
Noldorin
@Noldorin: Especially since that avoids the problem of `sealed` types - not a problem in this case, but...
Lucas Jones
I didn't know you could add in type aliases with a using statement, thanks!
Jake Pearson
@person-b: Indeed. You're not really adding functionality, so there's no functional reason to inherit anyway.
Noldorin
A: 

If you are accessing the values by the key, use dictionary, that is what is meant for. To reduce the overhead of the dictionaries, try and give them an initial capacity.

Alternatively (if both lists are rather small), you could implement a custom hashing object, something like this (except prettier):

public class MyDictionary
{
    private Dictionary<string, string> values;

    public MyDictionary()
    {
        values = new Dictionary<string, string>();
    }

    public void Add(int index, string key, string value)
    {
        int hash = ((0xFFFF) & index) * (0xFFFF) + (0xFFFF) & key.GetHashCode();
        values.Add(hash, value);
    }
    public string Get(int index, string key)
    {
        int hash = ((0xFFFF) & index) * (0xFFFF) + (0xFFFF) & key.GetHashCode();
        return values[hash];
    }
}
LorenVS
Interesting... too much for my needs though.
paulwhit
+3  A: 

Both List and Dictionary are pretty efficient, so I wouldn't think twice about using them. (Unless you're going to be storing a gazillion dictionaries in your list, but that's not very common.)

If you think that List<Dictionary<string, string>> is too much to type, you can say in your preamble

using LoDSS = System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, string>>;

Note that this is just an alias no subclassing needed.

Ruben
Apparently, this isn't completely correct, aliases can't stack on aliases so you have to using the following code instead:using LoDSS = System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, string>>;
Jake Pearson
I stand corrected; I've updated the answer to reflect this.
Ruben
A: 

For clarity I would wrap your KeyValuePair<string, string> into something shorter, e.g. StringPair and also define a shorthand for a list of StringPair. This will shorten your syntax and help readability IMHO.

public class StringPair : KeyValuePair<string, string> { }

public class StringPairList : List<stringPair> { }

..


var jaggedList = new List<StringPairList>();
jscharf
+2  A: 

Consider using aliasing for shorthand:

namespace Application
{
    using MyList = List<List<KeyValuePair<string, string>>>;

    public class Sample
    {
        void Foo()
        {
            var list = new MyList();
        }
    }
}
Nick Riggs
Didn't know about that, but this helped a lot. Thanks.
paulwhit