SortedList Class
Represents a collection of key/value pairs that are sorted by the keys and are accessible by both key and index.
Namespace: System.Collections
Assembly: System.dll
Inheritance
Object → SortedList
Implements
Remarks
A SortedList object is a collection of key/value pairs that are sorted by the keys and are accessible by both key and index. The keys in a SortedList object must be unique. The values can be duplicated.
The keys are sorted in ascending order. If a new element is added with a key that already exists, an ArgumentException is thrown.
SortedList maintains the sorted order of its elements. This means that when you add an element, it is inserted in the correct position to maintain the sort order.
SortedList is not synchronized (thread-safe). If you need thread-safe access to the SortedList collection, use the wrapper returned by the SynchronizedCollection method.
Constructors
- SortedList()
- Initializes a new instance of the
SortedListclass that is empty, has the default initial capacity, and is sorted by using the default comparer. - SortedList(Int32 capacity)
- Initializes a new instance of the
SortedListclass that is empty, has the specified initial capacity, and is sorted by using the default comparer. -
capacity- The initial number of elements that the
SortedListcan contain. - SortedList(IComparer comparer)
- Initializes a new instance of the
SortedListclass that is empty, has the default initial capacity, and is sorted by using the specifiedIComparer. -
comparer- The
IComparerto use for sorting the keys. If null, the default comparer is used. - SortedList(Int32 capacity, IComparer comparer)
- Initializes a new instance of the
SortedListclass that is empty, has the specified initial capacity, and is sorted by using the specifiedIComparer. -
capacity- The initial number of elements that the
SortedListcan contain.comparer- The
IComparerto use for sorting the keys. If null, the default comparer is used.
Properties
- Capacity : Int32 { get; set; }
- Gets or sets the number of elements that the
SortedListinstance can initially accommodate. - Count : Int32 { get; }
- Gets the number of key/value pairs contained in the
SortedListobject. - IsFixedSize : Boolean { get; }
- Gets a value indicating whether the
SortedListobject has a fixed size. - IsReadOnly : Boolean { get; }
- Gets a value indicating whether the
SortedListobject is read-only. - Keys : ICollection { get; }
- Gets an ICollection containing the keys in the
SortedListobject. - Values : ICollection { get; }
- Gets an ICollection containing the values in the
SortedListobject.
Methods
- Add(Object key, Object value) : Void
- Adds an element with the specified key and value to the
SortedListobject. -
key- The key of the element to add. Cannot be null.
value- The value of the element to add.
-
Example
using System.Collections; SortedList sl = new SortedList(); sl.Add("Apple", 1); sl.Add("Banana", 2); sl.Add("Cherry", 3); - Clear() : Void
- Removes all elements from the
SortedListobject. - Clone() : Object
- Creates a shallow copy of the
SortedListobject. - Contains(Object key) : Boolean
- Determines whether the
SortedListobject contains the specified key. -
key- The key to locate in the
SortedListobject. - ContainsKey(Object key) : Boolean
- Determines whether the
SortedListobject contains the specified key. (Equivalent toContains). -
key- The key to locate in the
SortedListobject. - ContainsValue(Object value) : Boolean
- Determines whether the
SortedListobject contains the specified value. -
value- The value to locate in the
SortedListobject. - GetByIndex(Int32 index) : Object
- Gets the value at the specified index.
-
index- The zero-based index of the element to get.
- GetKey(Int32 index) : Object
- Gets the key at the specified index.
-
index- The zero-based index of the element whose key is to be retrieved.
- IndexOfKey(Object key) : Int32
- Gets the zero-based index of the specified key in the
SortedListobject. -
key- The key to locate in the
SortedListobject. - IndexOfValue(Object value) : Int32
- Gets the zero-based index of the first occurrence of the specified value in the
SortedListobject. -
value- The value to locate in the
SortedListobject. - Remove(Object key) : Void
- Removes the element with the specified key from the
SortedListobject. -
key- The key of the element to remove.
- RemoveAt(Int32 index) : Void
- Removes the element at the specified index from the
SortedListobject. -
index- The zero-based index of the element to remove.
Interfaces
SortedList implements the following interfaces:
ICollection Members
IDictionary Members
KeysValuesIsFixedSizeIsReadOnlyItem[Object](Indexers)AddRemoveContains