C# - The SortedList Class

The SortedList class provides a number of properties and methods that you use to work with a sortedlist. The properties and methods are listed below.

Properties and Methods of the SortedList Class

Item Description
Properties
Capacity Contains the allocated length of the sortedlist
Count Contains the number of items currently in the sortedlist
IsReadOnly Contains True if the sortedlist is read-only and False otherwise
IsSynchronized Contains True if the arraylist is synchronized
Item[] Contains an index of all the items in the sortedlist
Keys Contains a collection of all keys in the sortedlist
Values Contains a collection of all values in the sortedlist
Methods
Add() Adds an item to the sortedlist and returns the newly added index
Clear() Clears all items from the sortedlist
Clone() Creates a duplicate of the sortedlist
Contains() Returns True if the given object is in the current sortedlist
ContainsKey() Returns a collection of all keys in the sortedlist
ContainsValue() Returns a collection of all values in the sortedlist
CopyTo() Copies all or part of the current sortedlist to another sortedlist
GetByIndex() Returns the value of a specific location in the sortedlist
GetKey() Returns the key of a specific location in the sortedlist
GetKeyList() Returns the collection of keys in the sortedlist
GetValueList() Returns the collection of values in the sortedlist
IndexOfKey() Returns the index of a specific key
IndexOfValue() Returns the index of a specific value
Remove() Removes an item from the sortedlist by key
RemoveAt() Removes an item from the sortedlist by index
SetByIndex() Sets the value of an item in the sortedlist by index
Synchronized() Returns a synchronized copy of the sortedlist
TrimToSize() Changes the size of the sortedlist to match the number of items currently in the sortedlist
SortedList.Capacity

Syntax

Int32 Capacity

Description

The SortedList.Capacity property contains the maximum size of the sortedlist. The default capacity of a sortedlist is 16.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

items.Capacity = 5;

MyLabel.Text = "Capacity is "+items.Capacity.ToString();
SortedList.Count

Syntax

Int32.Count

Description

The SortedList.Count property contains the current number of items in the sortedlist.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

MyLabel.Text = "Count is "+items.Count.ToString();
SortedList.IsReadOnly

Syntax

Boolean IsReadOnly

Description

The SortedList.IsReadOnly property returns true if the current sortedlist is read-only.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

MyLabel.Text = "IsReadOnly set to "+items.IsReadOnly;
SortedList.IsSynchronized

Syntax

Boolean IsSynchronized

Description

The SortedList.IsSynchronized property is True if the current sortedlist is synchronized.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

MyLabel.Text = "IsSynchronized set to "+items.IsSynchronized;
SortedList.Item

Syntax

Object Item ( Object key )

Description

The SortedList.Item property enabled a developer to retrieve a value by using a key as an index.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

MyLabel.Text = "Item of Key1 in SortedList: "+items["key1"];
SortedList.Keys

Syntax

ICollection Keys

Description

The SortedList.Keys property contains a collection of keys in the sortedlist.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

MyList.DataSource = items.Keys;
MyList.DataBind();
SortedList.Values

Syntax

ICollection Values

Description

The SortedList.Values property contains a collection of values in the sortedlist.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

MyList.DataSource = items.Values;
MyList.DataBind();
SortedList.Add()

Syntax

Void Add ( Object Key, Object value )

Description

The SortedList.Add() method adds key/value pairs to the sortedlist.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);
SortedList.Clear()

Syntax

void Clear()

Description

The SortedList.Clear() method removes all keys and values from the current sortedlist.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

items.Clear();
SortedList.Clone()

Syntax

Object Clone()

Description

The SortedList.Clone() method returns a copy of the current sortedlist.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

SortedList NewList = (SortedList)items.Clone();
SortedList.Contains()

Syntax

Boolean Contains( Object key )

Description

The SortedList.Contains() method returns True if the current sortedlist contains the key.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

MyLabel.Text = "Does list contain this key: "+items.Contains("key3").ToString();
SortedList.ContainsKey()

Syntax

Boolean ContainsKey( Object key )

Description

The SortedList.ContainsKey() method returns True if the current sortedlist contains the key.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

MyLabel.Text = "Does list contain this key: "+items.ContainsKey("key3").ToString();
SortedList.ContainsValue()

Syntax

Boolean ContainsValue( Object key )

Description

The SortedList.ContainsValue() method returns True if the current sortedlist contains the value.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

MyLabel.Text = "Does list contain this value: "+items.ContainsValue("5").ToString();
SortedList.CopyTo()

Syntax

void CopyTo( Array array, Int32 index )

Description

The SortedList.CopyTo() method copies the current sortedlist to an array.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

String[] MyArray = new String[4];
items.CopyTo(MyArray);
MyLabel.Text = "Third item of SortedList is "+MyArray[2];
SortedList.GetByIndex()

Syntax

Object GetByIndex( Int32 index )

Description

The SortedList.GetByIndex() method returns the value of a particular location in the list, given by the index argument.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

MyLabel.Text = "Value at position two of SortedList is "+items.GetByIndex(1);
SortedList.GetKey()

Syntax

Object GetKey( Int32 index )

Description

The SortedList.GetKey() method returns the key of the object at any location in the sortedlist that is given by the index argument.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

MyLabel.Text = "Key at position two of SortedList is "+items.GetKey(1);
SortedList.GetKeyList()

Syntax

IList GetKeyList()

Description

The SortedList.GetKeyList() method returns a list of all keys in the sortedlist.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

ArrayList MyArray = new ArrayList();
MyArray = ArrayList.Adapter(items.GetKeyList());
SortedList.GetValueList()

Syntax

IList GetValueList()

Description

The SortedList.GetValueList() method returns the list of all values in the sortedlist.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

ArrayList MyArray = new ArrayList();
MyArray = ArrayList.Adapter(items.GetValueList());
SortedList.IndexOfKey()

Syntax

Int32 IndexOfKey( Object key )

Description

The SortedList.IndexOfKey() method returns the index of the object with the given key.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

MyLabel.Text = "IndexOf 'key2' is: "+items.IndexOfKey("key2");
SortedList.IndexOfValue()

Syntax

Int32 IndexOfValue( Object value )

Description

The SortedList.IndexOfValue() method returns the index of the object with the given value. It returns -1 if the value is not found.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

MyLabel.Text = "IndexOf '9' is: "+items.IndexOfValue("9");
SortedList.Remove()

Syntax

Void Remove( Object key )

Description

The SortedList.Remove() method removes the object with the given key from the sortedlist.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

items.Remove("key1");
SortedList.RemoveAt()

Syntax

Void RemoveAt( Int32 index )

Description

The SortedList.RemoveAt() method removes an object from the sortedlist at the given index. If the SortedList.RemoveAt() method is attempted on an empty sortedlist, ArgumentOutOfRangeException is thrown at runtime.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

items.RemoveAt(2);
SortedList.SetByIndex()

Syntax

Void SetByIndex( Int32 index, Object value )

Description

The SortedList.SetByIndex() method enables a developer to set the value of any index in a sortedlist by index.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

items.SetByIndex(1, "newvalue");
SortedList.Synchronized()

Syntax

SortedList Synchronized( SortedList list )

Description

The SortedList.Synchronized() method returns a synchronized copy of the current sortedlist.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

items = SortedList.Synchronized(items);
MyLabel.Text = "True if items is synchronized: "+items.IsSynchronized;
SortedList.TrimToSize()

Syntax

Void TrimToSize()

Description

The SortedList.TrimToSize() method sets the capacity of the sortedlist to the current count of items in the sortedlist.

Example

SortedList items = new SortedList();
items.Add("key2", 5);
items.Add("key1", 2);
items.Add("key3", 9);

MyLabel.Text = "Current Capacity is "+items.Capacity.ToString();
items.TrimToSize();
MyLabel2.Text = "New Capacity is "+items.Capacity.ToString();