C
C#3mo ago
Gipper

How can I have a key-value data structure that allows duplicate keys?

Is that even possible, if so how? Should I just be avoiding this to begin with, if so how?
9 Replies
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Schreck
Schreck3mo ago
A key-value list with duplicate keys does not make sense to me. If you need it, just make a list. What advantages do you see in a key value list if you have duplicates?
canton7
canton73mo ago
Anything wrong with a list dictionary of lists? Also see linq's GroupBy
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Petris
Petris3mo ago
Json :when: Although not sure if duplicate keys are legal there tbh Also SortedList allows this
Evyr
Evyr3mo ago
You could use List<KeyValuePair<TKey, TValue>> as well
canton7
canton73mo ago
But why would you, when you can use a tuple?
Anton
Anton3mo ago
A tuple wouldn't implicitly cast to a kvp, so it's just for generic code to work with regular dictionaries as well as this. I see no other benefit.
hutoanhill
hutoanhill3mo ago
Can you explain your use case a little more? you could do a custom datatype thast a Dictionary<TKey, List<TValue>> under the hood. when you add a key value pair it appends the value to the list. thats close to what you want i think without knowing what your usecase is
Want results from more Discord servers?
Add your server