-4

I have to store list of two values similar to <string,string> and it is not like key,value.I can store it in objects or list,but whats the best way to store such data structures? I have seen few persons referring to use List<Tuple<string, string>> but what is the advantage of using above over other data structures like hashtable or object,

6
  • 7
    Well, List<string, string> doesn't exist. That's the main advantage Commented Mar 13, 2017 at 21:07
  • Does c# has List<string, string>() structure? Commented Mar 13, 2017 at 21:08
  • 1
    @AkashKC you're correct it absolutely doesnt Commented Mar 13, 2017 at 21:08
  • You can use List<KeyValuePair<string,string>> Commented Mar 13, 2017 at 21:11
  • 1
    A key which is not unique is ... eh ... an unfortunate name, don't you think? And eventually it can lead to confusion. Commented Mar 13, 2017 at 21:13

2 Answers 2

2

If you want a "key" to correspond to multiple values and efficiently find those values, then you can use Dictionary<string, List<string>>. This will be hashtable lookup by key into a list of values, so finding the values for a key will be O(1).

If you want to correlate the keys and values but don't need to look up by key then you can use List<Tuple<string, string>> or List<KeyValuePair<string, string>> if that suits you better, which you can iterate through and look up by index, but searching for a key will be O(N).

Sign up to request clarification or add additional context in comments.

Comments

0

Make a new class with two string properies and put it in the List instead of Tuple<string, string> or anything else weird construct. Main advantage is that everybody else knows whats inside the list and you dont have two magic strings. Cleanest and easiest way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.