Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I shorten List<List<KeyValuePair<string, string>>>?

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?

like image 573
paulwhit Avatar asked Aug 25 '09 17:08

paulwhit


3 Answers

Consider using aliasing for shorthand:

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

    public class Sample
    {
        void Foo()
        {
            var list = new MyList();
        }
    }
}
like image 170
Nick Riggs Avatar answered Nov 06 '22 23:11

Nick Riggs


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.

like image 40
Ruben Avatar answered Nov 07 '22 00:11

Ruben


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>>>;
like image 40
Jake Pearson Avatar answered Nov 06 '22 23:11

Jake Pearson