Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing duplicate key value pairs in C#

Tags:

c#

collections

I have a data like in (string , int) pair. How to store this data in collection object. Both values can be duplicate. Which collection object should i use??

EDIT: How can i access elements separately..??

like image 255
Royson Avatar asked Dec 18 '09 11:12

Royson


People also ask

Can key value pair have duplicate keys?

[C#] Dictionary with duplicate keysThe Key value of a Dictionary is unique and doesn't let you add a duplicate key entry. To accomplish the need of duplicates keys, i used a List of type KeyValuePair<> .

Can pair have duplicates?

If you're adding pairs to a List , you can even hold duplicates of pair entries.

Can we store duplicate keys in dictionary C#?

In Dictionary, the key cannot be null, but value can be. In Dictionary, key must be unique. Duplicate keys are not allowed if you try to use duplicate key then compiler will throw an exception. In Dictionary, you can only store same types of elements.

Can Collection store duplicate values?

You cannot do it by Java collection. You can use Multimap it supports duplicate keys but it also support duplicate keys and value pairs.


2 Answers

You can use List<KeyValuePair<string,int>>.

This will store a list of KeyValuePair's that can be duplicate.

like image 143
Oded Avatar answered Sep 18 '22 13:09

Oded


You can use List<KeyValuePair<string, int>> if you want to add & remove items, or KeyValuePair<string, int>[] if the number of items is known

like image 34
thecoop Avatar answered Sep 19 '22 13:09

thecoop