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

Definition

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

public:
 property TValue default[TKey] { TValue get(TKey key); void set(TKey key, TValue value); };
public TValue this[TKey key] { get; set; }
member this.Item('Key) : 'Value with get, set
Default Public Property Item(key As TKey) As TValue

Parameter

key
TKey

Der Schlüssel des Elements, das abgerufen oder festgelegt werden soll.

Eigenschaftswert

TValue

Das Element mit dem angegebenen Schlüssel.

Ausnahmen

key ist null.

Die Eigenschaft wird abgerufen, und key wird nicht gefunden.

Die Eigenschaft wird festgelegt, und IDictionary<TKey,TValue> ist schreibgeschützt.

Beispiele

Im folgenden Codebeispiel wird die Item[] Eigenschaft (der Indexer in C#) verwendet, um Werte abzurufen, die demonstrieren, dass ein KeyNotFoundException Angeforderter Schlüssel nicht vorhanden ist und zeigt, dass der wert, der einem Schlüssel zugeordnet ist, ersetzt werden kann.

Das Beispiel zeigt auch, wie Sie die TryGetValue Methode als effizientere Methode zum Abrufen von Werten verwenden, wenn ein Programm häufig wichtige Werte ausprobieren muss, die nicht im Wörterbuch enthalten sind.

Dieser Code ist Teil eines größeren Beispiels, das kompiliert und ausgeführt werden kann. Siehe System.Collections.Generic.IDictionary<TKey,TValue>.

// 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 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 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 indexer throws an exception if the requested key is
// not in the dictionary.
try
{
    Console::WriteLine("For key = \"tif\", value = {0}.",
        openWith["tif"]);
}
catch (KeyNotFoundException^)
{
    Console::WriteLine("Key = \"tif\" is not found.");
}
// The indexer throws an exception if the requested key is
// not in the dictionary.
try
{
    Console.WriteLine("For key = \"tif\", value = {0}.",
        openWith["tif"]);
}
catch (KeyNotFoundException)
{
    Console.WriteLine("Key = \"tif\" is not found.");
}
' The default Item property throws an exception if the requested
' key is not in the dictionary.
Try
    Console.WriteLine("For key = ""tif"", value = {0}.", _
        openWith("tif"))
Catch 
    Console.WriteLine("Key = ""tif"" is not found.")
End Try
// When a program often has to try keys that turn out not to
// be in the dictionary, TryGetValue can be a more efficient
// way to retrieve values.
String^ value = "";
if (openWith->TryGetValue("tif", value))
{
    Console::WriteLine("For key = \"tif\", value = {0}.", value);
}
else
{
    Console::WriteLine("Key = \"tif\" is not found.");
}
// When a program often has to try keys that turn out not to
// be in the dictionary, TryGetValue can be a more efficient
// way to retrieve values.
string value = "";
if (openWith.TryGetValue("tif", out value))
{
    Console.WriteLine("For key = \"tif\", value = {0}.", value);
}
else
{
    Console.WriteLine("Key = \"tif\" is not found.");
}
' When a program often has to try keys that turn out not to
' be in the dictionary, TryGetValue can be a more efficient 
' way to retrieve values.
Dim value As String = ""
If openWith.TryGetValue("tif", value) Then
    Console.WriteLine("For key = ""tif"", value = {0}.", value)
Else
    Console.WriteLine("Key = ""tif"" is not found.")
End If

Hinweise

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

Sie können die Item[] Eigenschaft auch verwenden, um neue Elemente hinzuzufügen, indem Sie den Wert eines Schlüssels festlegen, der im Wörterbuch nicht vorhanden ist, zmyCollection["myNonexistentKey"] = myValue. B. in C# (myCollection("myNonexistentKey") = myValue in Visual Basic). 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 Add Methode keine vorhandenen Elemente.

Implementierungen können variieren, wie sie die Gleichheit von Objekten bestimmen; Die Klasse verwendet Comparer<T>.Defaultz. BList<T>. die Klasse, während der Dictionary<TKey,TValue> Benutzer die Implementierung angeben kann, die IComparer<T> zum Vergleichen von Schlüsseln verwendet werden soll.

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

Implementierungen können variieren, ob sie zulassen key null.

Gilt für:

Siehe auch