N
The Daily Insight

Does sorted list allow duplicate keys?

Author

Eleanor Gray

Updated on May 19, 2026

Does sorted list allow duplicate keys?

A SortedList object internally maintains two arrays to store the elements of the list, i.e, one array for the keys and another array for the associated values. The capacity of a SortedList object is the number of elements the SortedList can hold. A SortedList does not allow duplicate keys.

Can sorted set have duplicates C#?

Remarks. The SortedSet class does not accept duplicate elements.

Which collection can have duplicate keys C#?

In short: The easiest way to go would be a generic List collection while skiping the ArrayList class. Because, there are some performance considerations that you need to take into account. In addition, you can also use List> . This will store a list of KeyValuePair ‘s that can be duplicate.

In what way sorted list will add the values in C#?

The SortedList will store keys of int type and values of string type. The Add() method is used to add a single key-value pair in a SortedList . Keys cannot be null or duplicate. If found, it will throw a run-time exception.

How do you sort an array in C#?

The simplest way to sort an array in C# is using Array. Sort method. The Array. Sort method takes a one-dimensional array as an input and sorts the array elements in the ascending order.

What is a sorted list in C#?

In C#, SortedList is a collection of key/value pairs which are sorted according to keys. By default, this collection sort the key/value pairs in ascending order. It is of both generic and non-generic type of collection.

Can we add duplicate keys in Hashtable C#?

In hashtable, you can store elements of the same type and of the different types. The elements of hashtable that is a key/value pair are stored in DictionaryEntry, so you can also cast the key/value pairs to a DictionaryEntry. In Hashtable, key must be unique. Duplicate keys are not allowed.

Does KeyValuePair allow duplicates C#?

3 Answers. You can use List> . This will store a list of KeyValuePair ‘s that can be duplicate. If you want to avoid repeating the key for multiple values you can use Dictionary> .

Does C# list maintain order?

5 Answers. The List<> class does guarantee ordering – things will be retained in the list in the order you add them, including duplicates, unless you explicitly sort the list. According to MSDN: List “Represents a strongly typed list of objects that can be accessed by index.”

Are there sorted lists in C#?

What is SortedSet in C#?

In C#, SortedSet is a collection of objects in sorted order. It is of the generic type collection and defined under System. Collections. Generic namespace. It also provides many mathematical set operations, such as intersection, union, and difference.