SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace. SortedList.Add(Object, Object) Method is used to add an element with the specified key and value to a SortedList object.
Properties of SortedList:
- Internally the object of SortedList maintain the two arrays. First array is used to store the elements of the list i.e. keys and the second one is used to store the associated values.
- A key cannot be null but value can be.
- As SortedList used sorting which makes it slower in compare to Hashtable.
- The capacity of a SortedList can be dynamically increased through reallocation.
- The keys in the SortedList cannot be duplicated but values can be.
- The SortedList can be sorted according to the keys using the IComparer(Either in ascending or descending order).
public virtual void Add (object key, object value);
key: It is the key of the element which is to be added.
value: It is the value of the element which is to be added. The value can be null.
- ArgumentNullException: If the key is null.
- ArgumentException: If the element with the specified key is already exists in the SortedList object or SortedList is set to use the IComparable interface and the key does not implement the IComparable interface.
- NotSupportedException: If the The SortedList is read-only or has a fixed size.
- OutOfMemoryException: If there is not enough available memory in the system to add the pairs to the SortedList.
- InvalidOperationException: If the comparer throws an exception.
Below programs illustrate the use of above discussed method:
Chemistry: 96 CSE : 100 English : 99 Maths : 98 Physics : 97
System.ArgumentException: Item has already been added. Key in dictionary: ‘Chemistry’ Key being added: ‘Chemistry’
at System.Collections.SortedList.Add (System.Object key, System.Object value) in :0
at Geeks.Main (System.String args) in :0
- C# | Get or set the value associated with specified key in SortedList
- C# | How to create a SortedList
- C# SortedList with Examples
- C# | SortedList Class
- C# | Capacity of a SortedList
- C# | Remove the element with the specified key from a SortedList
- C# | Getting index of the specified value in a SortedList object
- C# | Remove all elements from a SortedList
- C# | Remove from the specified index of a SortedList
- C# | Getting the index of the specified key in a SortedList object
- C# | Search in a SortedList object
- C# | Getting the key at the specified index of a SortedList object
- Difference between SortedList and SortedDictionary in C#
- C# | Get an enumerator that iterates through the SortedList
- C# | Getting the Values in a SortedList object
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.