SortedDictionary<TKey,TValue>.IDictionary.Item[Object] Eigenschaft

Definition

Ruft das Element mit dem angegebenen Schlüssel ab oder legt dieses fest.

property System::Object ^ System::Collections::IDictionary::Item[System::Object ^] { System::Object ^ get(System::Object ^ key); void set(System::Object ^ key, System::Object ^ value); };
object System.Collections.IDictionary.Item[object key] { get; set; }
object? System.Collections.IDictionary.Item[object key] { get; set; }
member this.System.Collections.IDictionary.Item(obj) : obj with get, set
 Property Item(key As Object) As Object Implements IDictionary.Item

Parameter

key
Object

Der Schlüssel des abzurufenden Elements.

Eigenschaftswert

Object

Das Element mit dem angegebenen Schlüssel oder null, wenn key nicht im Wörterbuch enthalten ist oder key einen Typ aufweist, der dem Schlüsseltyp TKey des SortedDictionary<TKey,TValue> nicht zugeordnet werden kann.

Implementiert

Ausnahmen

key ist null.

Es wird ein Wert zugewiesen, und key ist ein Typ, der dem Schlüsseltyp TKey des SortedDictionary<TKey,TValue> nicht zugeordnet werden kann.

- oder - Es wird ein Wert zugewiesen, und value ist ein Typ, der dem Werttyp TValue des SortedDictionary<TKey,TValue> nicht zugeordnet werden kann.

Beispiele

Im folgenden Codebeispiel wird gezeigt, wie die IDictionary.Item[] Eigenschaft (der Indexer in C#) der System.Collections.IDictionary Schnittstelle mit einer SortedDictionary<TKey,TValue>Schnittstelle verwendet wird und wie sich die Eigenschaft von der SortedDictionary<TKey,TValue>.Item[] Eigenschaft unterscheidet.

Das Beispiel zeigt, dass die Eigenschaft wie die SortedDictionary<TKey,TValue>.Item[] SortedDictionary<TKey,TValue>.IDictionary.Item[] Eigenschaft den mit einem vorhandenen Schlüssel verknüpften Wert ändern kann und verwendet werden kann, um ein neues Schlüssel/Wertpaar hinzuzufügen, wenn der angegebene Schlüssel nicht im Wörterbuch enthalten ist. Im Beispiel wird auch gezeigt, dass die SortedDictionary<TKey,TValue>.IDictionary.Item[] Eigenschaft im Gegensatz zu der SortedDictionary<TKey,TValue>.Item[] Eigenschaft keine Ausnahme auslöst, wenn key sie nicht im Wörterbuch enthalten ist und stattdessen einen NULL-Verweis zurückgibt. Schließlich veranschaulicht das Beispiel, dass das Abrufen der SortedDictionary<TKey,TValue>.IDictionary.Item[] Eigenschaft einen NULL-Verweis zurückgibt, wenn nicht der richtige Datentyp ist, und dass durch das Festlegen der Eigenschaft eine Ausnahme ausgelöst wird, wenn key key der richtige Datentyp nicht vorhanden ist.

Das Codebeispiel ist Teil eines größeren Beispiels, einschließlich der Ausgabe, die für die IDictionary.Add Methode bereitgestellt wird.

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

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

        // Add some elements to the dictionary. 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");
Imports System.Collections
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new sorted dictionary of strings, with string keys,
        ' and access it using the IDictionary interface.
        '
        Dim openWith As IDictionary = _
            New SortedDictionary(Of String, String)
        
        ' Add some elements to the dictionary. 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.");
}
' The Item property is the default property, so you 
' can omit its name when accessing elements. 
Console.WriteLine("For key = ""rtf"", value = {0}.", _
    openWith("rtf"))

' The default Item property 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 default Item property
' for that key adds a new key/value pair.
openWith("doc") = "winword.exe"

' The default Item property returns Nothing if the key
' is of the wrong data type.
Console.WriteLine("The default Item property returns Nothing" _
    & " if the key is of the wrong type:")
Console.WriteLine("For key = 2, value = {0}.", _
    openWith(2))

' The default Item property 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 
    Console.WriteLine("A key of the wrong type was specified" _
        & " when setting the default Item property.")
End Try
// Unlike the default Item property on the Dictionary class
// itself, IDictionary.Item does not throw an exception
// if the requested key is not in the dictionary.
Console.WriteLine("For key = \"tif\", value = {0}.",
    openWith["tif"]);
' Unlike the default Item property on the Dictionary class
' itself, IDictionary.Item does not throw an exception
' if the requested key is not in the dictionary.
Console.WriteLine("For key = ""tif"", value = {0}.", _
    openWith("tif"))
    }
}

    End Sub

End Class

Hinweise

Diese Eigenschaft bietet die Möglichkeit, mithilfe der folgenden C#-Syntax auf ein bestimmtes Element in der Auflistung zuzugreifen: myCollection[key] (myCollection(key) in Visual Basic).

Sie können auch die Item[] Eigenschaft zum Hinzufügen neuer Elemente durch Festlegen der Wert eines Schlüssels, der nicht vorhanden ist, im Wörterbuch enthalten z. B. myCollection["myNonexistentKey"] = myValue Wenn der angegebene Schlüssel jedoch bereits im Wörterbuch vorhanden ist, überschreibt das Festlegen der Item[] Eigenschaft den alten Wert. Im Gegensatz dazu ändert die IDictionary.Add Methode keine vorhandenen Elemente.

Die C#-Sprache verwendet dieses Schlüsselwort, um die Indexer zu definieren, anstatt die IDictionary.Item[] Eigenschaft zu implementieren. Visual Basic implementiert IDictionary.Item[] als Standardeigenschaft und stellt auf diese Weise dieselbe Indizierungsfunktionalität bereit.

Beim Abrufen des Werts dieser Eigenschaft handelt es sich um einen O(log n)-Vorgang. Das Festlegen der Eigenschaft ist auch ein O(log n)-Vorgang.

Gilt für:

Siehe auch