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

I want to keep a list of lists of value key pairs in an easy structure. It seems too cumbersome. What's better? Does the list <Dictionary <string, string →> add a lot of overhead? What other options are available?

+3
source share
5 answers

Consider using aliases to shorten:

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

    public class Sample
    {
        void Foo()
        {
            var list = new MyList();
        }
    }
}
+10
source

Both Listand Dictionaryare quite effective, so I would not have to think twice to use them. (If you are not going to keep gazillion dictionaries on your list, but this is not very common.)

, List<Dictionary<string, string>> ,

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

, , .

+3

< string, string > List < KeyValuePair < , → , , . , , - . - :

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

using :

using MyShorthand = System.Collections.Generic.List<System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, string>>>;
+2

, , , . , .

( ), , - ( ):

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

For clarity, I would turn yours KeyValuePair<string, string>into something shorter, for example. StringPair, and also define an abbreviation for the list StringPair. This will shorten your syntax and help read IMHO.

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

public class StringPairList : List<stringPair> { }

..


var jaggedList = new List<StringPairList>();
0
source

Source: https://habr.com/ru/post/1716020/


All Articles