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

Definition

Dient zum Abrufen oder Festlegen des Werts mit dem angegebenen Schlüssel.

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; }
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 Werts.

Eigenschaftswert

Der wert, der dem angegebenen Schlüssel zugeordnet ist oder sich nicht im Wörterbuch befindet oder keynullkey ein Typ ist, der dem Schlüsseltyp TKey des Schlüssels Dictionary<TKey,TValue>nicht zugewiesen werden kann.

Implementiert

Ausnahmen

key ist null.

Ein Wert wird zugewiesen und key weist einen Typ auf, der dem Schlüsseltyp TKey des Typs Dictionary<TKey,TValue>nicht zugewiesen werden kann.

-oder-

Ein Wert wird zugewiesen und weist einen Typ auf, der dem Werttyp TValue des Dictionary<TKey,TValue>Typs nicht zugewiesen werden kann.

Beispiele

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

Das Beispiel zeigt, dass die Eigenschaft wie die Dictionary<TKey,TValue>.Item[]Dictionary<TKey,TValue>.IDictionary.Item[] Eigenschaft den einem vorhandenen Schlüssel zugeordneten Wert ändern kann und verwendet werden kann, um ein neues Schlüssel-Wert-Paar hinzuzufügen, wenn sich der angegebene Schlüssel nicht im Wörterbuch befindet. Das Beispiel zeigt auch, dass die Dictionary<TKey,TValue>.IDictionary.Item[] Eigenschaft im Gegensatz zu der Dictionary<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 wird im Beispiel veranschaulicht, dass beim Abrufen der Dictionary<TKey,TValue>.IDictionary.Item[] Eigenschaft ein Nullverweis zurückgegeben wird, wenn key es sich nicht um den richtigen Datentyp handelt, und durch das Festlegen der Eigenschaft wird eine Ausnahme ausgelöst, wenn key es sich nicht um den richtigen Datentyp handelt.

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

public class Example
{
    public static void Main()
    {
        // Create a new dictionary of strings, with string keys,
        // and access it using the IDictionary interface.
        //
        IDictionary openWith = new Dictionary<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 = {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 = {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 = {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 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 = {openWith["tif"]}.""");
    }
}
open System
open System.Collections
open System.Collections.Generic

// Create a new dictionary of strings, with string keys,
// and access it using the IDictionary interface.
let openWith: IDictionary = Dictionary<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.
printfn $"""For key = "rtf", value = {openWith.["rtf"]}."""

// The indexer can be used to change the value associated
// with a key.
openWith["rtf"] <- "winword.exe"
printfn $"""For key = "rtf", value = {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.
printfn "The indexer returns null if the key is of the wrong type:"
printfn $"""For key = 2, value = {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."
with :? ArgumentException ->
    printfn "A key of the wrong type was specified when assigning to the indexer."

// 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.
printfn $"""For key = "tif", value = {openWith.["tif"]}."""
Imports System.Collections
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new dictionary of strings, with string keys,
        ' and access it using the IDictionary interface.
        '
        Dim openWith As IDictionary = _
            New Dictionary(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 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"))

    End Sub

End Class

Hinweise

Diese Eigenschaft bietet die Möglichkeit, mithilfe der folgenden C#-Syntax auf einen bestimmten Wert 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 nicht im Wörterbuch vorhanden ist, myCollection["myNonexistentKey"] = myValuez. B. . . 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.

Die C#-Sprache verwendet dieses Schlüsselwort, um die Indexer zu definieren, anstatt die IDictionary.Item[] Eigenschaft zu implementieren. Visual Basic wird als Standardeigenschaft implementiert IDictionary.Item[] , die die gleiche Indizierungsfunktion bereitstellt.

Das Abrufen oder Festlegen des Werts dieser Eigenschaft nähert sich einem O(1)-Vorgang.

Gilt für:

Weitere Informationen