Question

Is it possible to add items to sorted list with the same value, because when I am trying to do so it shows me an error:
"An entry with the same key already exists."
If it's possible, then how?

Was it helpful?

Solution

It is not possible* to add duplicate keys as stated by other users.

In c# you might be able to use the Lookup class instead, which allows multiple values to be stored with the same key.

See: http://msdn.microsoft.com/en-us/library/bb460184.aspx

* It is possible, see comments, but only by defining a comparitor that never returns equality for equal items, which IMO is a really really bad idea.

OTHER TIPS

No, it's not possible. The keys must be unique.

Key should be unique. See this in MSDN

ArgumentException   - An element with the specified key already exists in the SortedList object.

http://msdn.microsoft.com/en-us/library/system.collections.sortedlist.add.aspx

Define a class that implements IComparer. When you instantiate the SortedList, you pass in an instance of your class. Check out Knasterbax's answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top