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

Definice

Získá nebo nastaví prvek se zadaným klíčem.

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

Parametry

key
Object

Klíč prvku, který chcete získat nebo nastavit.

Hodnota vlastnosti

Object

Prvek se zadaným klíčem nebo null pokud key není ve slovníku nebo key je typu, který nelze přiřadit k typu klíče .TKey SortedList<TKey,TValue>

Implementuje

Výjimky

key je null.

Je přiřazena hodnota a key je typu, který nelze přiřadit k typu klíče .TKey SortedList<TKey,TValue>

-nebo- Je přiřazena hodnota a value je typu, který nelze přiřadit k typu hodnoty .TValue SortedList<TKey,TValue>

Příklady

Následující příklad kódu ukazuje, jak použít IDictionary.Item[] vlastnost (indexer v jazyce C#) System.Collections.IDictionary rozhraní s SortedList<TKey,TValue>a způsoby, jak se vlastnost liší od SortedList<TKey,TValue>.Item[] vlastnosti.

Příklad ukazuje, že podobně jako SortedList<TKey,TValue>.Item[] vlastnost může vlastnost změnit hodnotu přidruženou k existujícímu klíči a lze ji použít k přidání nového páru klíč/hodnota, SortedList<TKey,TValue>.IDictionary.Item[] pokud zadaný klíč není v seznamu seřazených. Příklad také ukazuje, že na rozdíl od SortedList<TKey,TValue>.Item[] vlastnosti tato vlastnost nevyvolá výjimku, SortedList<TKey,TValue>.IDictionary.Item[] pokud key není v seřazeného seznamu, vrátí místo toho odkaz null. Nakonec příklad ukazuje, že získání SortedList<TKey,TValue>.IDictionary.Item[] vlastnosti vrátí hodnotu null odkaz, pokud není správný datový typ a že nastavení vlastnosti vyvolá výjimku, pokud key key není správný datový typ.

Příklad kódu je součástí většího příkladu, včetně výstupu, poskytnutého pro metodu IDictionary.Add .

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");
Imports System.Collections
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new sorted list of strings, with string keys,
        ' and access it using the IDictionary interface.
        '
        Dim openWith As IDictionary = _
            New sortedList(Of 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.");
}
' 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 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"]);
' Unlike the default Item property on the SortedList 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"))
    }
}

    End Sub

End Class

Poznámky

Tato vlastnost se vrátínull, pokud key je typu, který nelze přiřadit k typu klíče .TKey SortedList<TKey,TValue>

Tato vlastnost poskytuje možnost získat přístup ke konkrétnímu prvku v kolekci pomocí následující syntaxe: myCollection[key].

Vlastnost můžete také použít Item[] k přidání nových prvků nastavením hodnoty klíče, který ve slovníku neexistuje, například myCollection["myNonexistentKey"] = myValue. Pokud však zadaný klíč již ve slovníku existuje, nastavení Item[] vlastnosti přepíše starou hodnotu. Naproti tomu Add metoda neupravuje existující prvky.

Jazyk C# používá toto klíčové slovo k definování indexerů místo implementace IDictionary.Item[] vlastnosti. Visual Basic implementuje IDictionary.Item[] jako výchozí vlastnost, která poskytuje stejné funkce indexování.

Načtení hodnoty této vlastnosti je operace O(log n), kde n je Count. Nastavení vlastnosti je operace O(log n), pokud je klíč již v souboru SortedList<TKey,TValue>. Pokud klíč není v seznamu, nastavení vlastnosti je operace O(n) pro neseřazená data nebo O(log n), pokud je nový prvek přidán na konec seznamu. Pokud vložení způsobí změnu velikosti, operace je O(n).

Platí pro

Viz také