This documentation is archived and is not being maintained.

SortedList.Item Property

Gets and sets the value associated with a specific key in the SortedList.

[C#] In C#, this property is the indexer for the SortedList class.

[Visual Basic]
Public Overridable Default Property Item( _
   ByVal key As Object _
) As Object  Implements IDictionary.Item
public virtual object this[
 object key
] {get; set;}
public: __property virtual Object* get_Item(
 Object* key
public: __property virtual void set_Item(
 Object* key,
returnValue = SortedListObject.Item(key);
SortedListObject.Item(key) = returnValue;
returnValue = SortedListObject(key);
SortedListObject(key) = returnValue;

[JScript] In JScript, you can use the default indexed properties defined by a type, but you cannot explicitly define your own. However, specifying the expando attribute on a class automatically provides a default indexed property whose type is Object and whose index type is String.

Arguments [JScript]

The key associated with the value to get or set.

Parameters [Visual Basic, C#, C++]

The key associated with the value to get or set.

Property Value

The value associated with key in the SortedList, if key is found; otherwise, a null reference (Nothing in Visual Basic).




Exception Type Condition
ArgumentNullException key is a null reference (Nothing in Visual Basic).
NotSupportedException The property is set and the SortedList is read-only.


The property is set, key does not exist in the collection, and the SortedList has a fixed size.

InvalidOperationException The comparer throws an exception.


This property provides the ability to access a specific element in the collection by using the following syntax: myCollection[key].

When setting this property, if the specified key already exists in the SortedList, the value is replaced; otherwise, a new element is created. In contrast, the Add method does not modify existing elements.

A key cannot be a null reference (Nothing in Visual Basic), but a value can be. To distinguish between a null reference (Nothing) that is returned because the specified key is not found and a null reference (Nothing) that is returned because the value of the specified key is a null reference (Nothing), use the Contains method or the ContainsKey method to determine if the key exists in the list.

The elements of a SortedList are sorted by the keys either according to a specific IComparer implementation specified when the SortedList is created or according to the IComparable implementation provided by the keys themselves.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

SortedList Class | SortedList Members | System.Collections Namespace | Add | Contains | ContainsKey | Performing Culture-Insensitive String Operations in Collections