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

Definitie

Hiermee haalt u de waarde op of stelt u deze in met de opgegeven sleutel.

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

Parameters

key
Object

De sleutel van de waarde die moet worden opgehaald.

Waarde van eigenschap

De waarde die is gekoppeld aan de opgegeven sleutel, of null als key deze niet in de woordenlijst staat of key van een type is dat niet kan worden toegewezen aan het sleuteltype TKey van het Dictionary<TKey,TValue>.

Implementeringen

Uitzonderingen

key is null.

Er wordt een waarde toegewezen en key is van een type dat niet kan worden toegewezen aan het sleuteltype TKey van de Dictionary<TKey,TValue>.

– of –

Er wordt een waarde toegewezen en is van een type dat niet kan worden toegewezen aan het waardetype TValue van de Dictionary<TKey,TValue>.

Voorbeelden

In het volgende codevoorbeeld ziet u hoe u de IDictionary.Item[] eigenschap (de indexeerfunctie in C#) van de System.Collections.IDictionary interface gebruikt met een Dictionary<TKey,TValue>, en hoe de eigenschap verschilt van de Dictionary<TKey,TValue>.Item[] eigenschap.

In het voorbeeld ziet u dat, zoals de Dictionary<TKey,TValue>.Item[] eigenschap, de Dictionary<TKey,TValue>.IDictionary.Item[] eigenschap de waarde kan wijzigen die is gekoppeld aan een bestaande sleutel en kan worden gebruikt om een nieuw sleutel-/waardepaar toe te voegen als de opgegeven sleutel niet in de woordenlijst staat. In het voorbeeld ziet u ook dat in tegenstelling tot de Dictionary<TKey,TValue>.Item[] eigenschap de Dictionary<TKey,TValue>.IDictionary.Item[] eigenschap geen uitzondering genereert als key deze zich niet in de woordenlijst bevindt en in plaats daarvan een null-verwijzing retourneert. Ten slotte laat het voorbeeld zien dat het ophalen van de Dictionary<TKey,TValue>.IDictionary.Item[] eigenschap een null-verwijzing retourneert als key dit niet het juiste gegevenstype is en dat het instellen van de eigenschap een uitzondering genereert als key dit niet het juiste gegevenstype is.

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

Opmerkingen

Deze eigenschap biedt de mogelijkheid om toegang te krijgen tot een specifieke waarde in de verzameling met behulp van de volgende C#-syntaxis: myCollection[key] (myCollection(key) in Visual Basic).

U kunt de Item[] eigenschap ook gebruiken om nieuwe elementen toe te voegen door de waarde in te stellen van een sleutel die niet bestaat in de woordenlijst, bijvoorbeeld myCollection["myNonexistentKey"] = myValue. Als de opgegeven sleutel echter al bestaat in de woordenlijst, wordt de oude waarde overschreven door de Item[] eigenschap in te stellen. De methode wijzigt daarentegen Add geen bestaande elementen.

De C#-taal gebruikt dit trefwoord om de indexeerfuncties te definiëren in plaats van de IDictionary.Item[] eigenschap te implementeren. Visual Basic implementeert IDictionary.Item[] als een standaardeigenschap, die dezelfde indexeringsfunctionaliteit biedt.

Het ophalen of instellen van de waarde van deze eigenschap benadert een O(1)-bewerking.

Van toepassing op

Zie ook