SortedList class

SortedList -Collection-class-tutorialbyte

The SortedList class represents a collection of key-and-value pairs that are sorted by the keys and are accessible by key and by index.

A sorted list is a combination of an array and a hashtable. It contains a list of items that can be accessed using a key or an index.

If you access items using an index, it is an ArrayList, and if you access items using a key, it is a Hashtable. Also the collection of items is always sorted by the key value.

Properties of the SortedList Class

The following table lists some of the commonly used properties of the SortedList class:

Property Description
Capacity Gets or sets the capacity of the SortedList.
Count Gets the number of elements contained in the SortedList.
IsFixedSize Gets a value indicating whether the SortedList has a fixed size.
IsReadOnly Gets a value indicating whether the SortedList is read-only.
Item Gets and sets the value associated with a specific key in the SortedList.
Keys Gets the keys in the SortedList.
Values Gets the values in the SortedList.

 

Methods of the SortedList Class

So the following table lists some of the commonly used methods of the SortedList class:

S.N Method Name & Purpose
1 Public Overridable Sub Add (key As Object, value As Object)

Adds an element with the specified key and value into the SortedList.

2 Public Overridable Sub Clear

Removes all elements from the SortedList.

3 Public Overridable Function ContainsKey (key As Object) As Boolean

Determines whether the SortedList contains a specific key.

4 Public Overridable Function ContainsValue (value As Object) As Boolean

Determines whether the SortedList contains a specific value.

5 Public Overridable Function GetByIndex (index As Integer) As Object

Gets the value at the specified index of the SortedList.

6 Public Overridable Function GetKey (index As Integer) As Object

Gets the key at the specified index of the SortedList.

7 Public Overridable Function GetKeyList As IList

Gets the keys in the SortedList.

8 Public Overridable Function GetValueList As IList

Gets the values in the SortedList.

9 Public Overridable Function IndexOfKey (key As Object) As Integer

Returns the zero-based index of the specified key in the SortedList.

10 Public Overridable Function IndexOfValue (value As Object ) As Integer

Returns the zero-based index of the first occurrence of the specified value in the SortedList.

11 Public Overridable Sub Remove (key As Object)

Removes the element with the specified key from the SortedList.

12 Public Overridable Sub RemoveAt (index As Integer)

This Property Removes the element at the specified index of SortedList.

13 Public Overridable Sub TrimToSize

Sets the capacity to the actual number of elements in the SortedList.

In addition to the above properties you can also check the following example of Sortedlist class

SortedList -Collection-class-tutorialbyte SortedList -Collection-class-tutorialbyte

Leave a Reply

Your email address will not be published. Required fields are marked *