SortedList<TKey,TValue>.IDictionary.Item[Object] Property

Definition

Gets or sets the element with the specified key.

object System.Collections.IDictionary.Item[object key] { get; set; }
object? System.Collections.IDictionary.Item[object key] { get; set; }

Parameters

key
Object

The key of the element to get or set.

Property Value

The element with the specified key, or null if key is not in the dictionary or key is of a type that is not assignable to the key type TKey of the SortedList<TKey,TValue>.

Implements

Exceptions

key is null.

A value is being assigned, and key is of a type that is not assignable to the key type TKey of the SortedList<TKey,TValue>.

-or-

A value is being assigned and is of a type that isn't assignable to the value type TValue of the SortedList<TKey,TValue>.

Examples

The following code example shows how to use the IDictionary.Item[] property (the indexer in C#) of the System.Collections.IDictionary interface with a SortedList<TKey,TValue>, and ways the property differs from the SortedList<TKey,TValue>.Item[] property.

The example shows that, like the SortedList<TKey,TValue>.Item[] property, the SortedList<TKey,TValue>.IDictionary.Item[] property can change the value associated with an existing key and can be used to add a new key/value pair if the specified key is not in the sorted list. The example also shows that unlike the SortedList<TKey,TValue>.Item[] property, the SortedList<TKey,TValue>.IDictionary.Item[] property does not throw an exception if key is not in the sorted list, returning a null reference instead. Finally, the example demonstrates that getting the SortedList<TKey,TValue>.IDictionary.Item[] property returns a null reference if key is not the correct data type, and that setting the property throws an exception if key is not the correct data type.

The code example is part of a larger example, including output, provided for the IDictionary.Add method.

using System;
using System.Collections;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new sorted list of strings, with string keys,
        // and access it using the IDictionary interface.
        //
        IDictionary openWith = new SortedList<string, string>();

        // Add some elements to the sorted list. There are no
        // duplicate keys, but some of the values are duplicates.
        // IDictionary.Add throws an exception if incorrect types
        // are supplied for key or value.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("dib", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");
// The Item property is another name for the indexer, so you
// can omit its name when accessing elements.
Console.WriteLine("For key = \"rtf\", value = {0}.",
    openWith["rtf"]);

// The indexer can be used to change the value associated
// with a key.
openWith["rtf"] = "winword.exe";
Console.WriteLine("For key = \"rtf\", value = {0}.",
    openWith["rtf"]);

// If a key does not exist, setting the indexer for that key
// adds a new key/value pair.
openWith["doc"] = "winword.exe";

// The indexer returns null if the key is of the wrong data
// type.
Console.WriteLine("The indexer returns null"
    + " if the key is of the wrong type:");
Console.WriteLine("For key = 2, value = {0}.",
    openWith[2]);

// The indexer throws an exception when setting a value
// if the key is of the wrong data type.
try
{
    openWith[2] = "This does not get added.";
}
catch (ArgumentException)
{
    Console.WriteLine("A key of the wrong type was specified"
        + " when assigning to the indexer.");
}
// Unlike the default Item property on the SorteList class
// itself, IDictionary.Item does not throw an exception
// if the requested key is not in the sorted list.
Console.WriteLine("For key = \"tif\", value = {0}.",
    openWith["tif"]);
    }
}

Remarks

This property returns null if key is of a type that is not assignable to the key type TKey of the SortedList<TKey,TValue>.

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

You can also use the Item[] property to add new elements by setting the value of a key that does not exist in the dictionary; for example, myCollection["myNonexistentKey"] = myValue. However, if the specified key already exists in the dictionary, setting the Item[] property overwrites the old value. In contrast, the Add method does not modify existing elements.

The C# language uses the this keyword to define the indexers instead of implementing the IDictionary.Item[] property. Visual Basic implements IDictionary.Item[] as a default property, which provides the same indexing functionality.

Retrieving the value of this property is an O(log n) operation, where n is Count. Setting the property is an O(log n) operation if the key is already in the SortedList<TKey,TValue>. If the key is not in the list, setting the property is an O(n) operation for unsorted data, or O(log n) if the new element is added at the end of the list. If insertion causes a resize, the operation is O(n).

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

See also