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,
![](../../users/profiles/1752335.webp)
- 3,318
- 10
- 40
- 74
-
7Well, `List
` doesn't exist. That's the main advantage – Kevin Gosse Mar 13 '17 at 21:07 -
Does c# has List
() structure? – Akash KC Mar 13 '17 at 21:08 -
1@AkashKC you're correct it absolutely doesnt – Ousmane D. Mar 13 '17 at 21:08
-
You can use `List
>` – Akash KC Mar 13 '17 at 21:11 -
1A `key` which is not unique is ... eh ... an unfortunate name, don't you think? And eventually it can lead to confusion. – Giorgos Altanis Mar 13 '17 at 21:13
-
like this? http://stackoverflow.com/questions/22297393/how-to-add-duplicate-keys-into-the-dictionary – hatchet - done with SOverflow Mar 13 '17 at 21:21
2 Answers
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).
![](../../users/profiles/6785695.webp)
- 5,545
- 1
- 8
- 18
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.
![](../../users/profiles/2388943.webp)
- 5,609
- 5
- 27
- 47