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

Definice

Získá nebo nastaví hodnotu přidruženou k zadanému klíči.

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

Parametry

key
TKey

Klíč, jehož hodnotu chcete získat nebo nastavit.

Hodnota vlastnosti

TValue

Hodnota přidružená k zadanému klíči. Pokud zadaný klíč není nalezen, operace get vyvolá KeyNotFoundException operaci a operace set vytvoří nový prvek pomocí zadaného klíče.

Implementuje

Výjimky

key je null.

Vlastnost se načte a key v kolekci neexistuje.

Příklady

Následující příklad kódu používá Item[] vlastnost (indexer v jazyce C#) k načtení hodnot, což demonstruje, že KeyNotFoundException je vyvolána, když není k dispozici požadovaný klíč, a ukazuje, že hodnota přidružená ke klíči může být nahrazena.

Příklad také ukazuje, jak použít metodu TryGetValue jako efektivnější způsob načtení hodnot, pokud program často musí vyzkoušet klíčové hodnoty, které nejsou v seřazených seznamu.

Tento příklad kódu je součástí většího příkladu SortedList<TKey,TValue> pro třídu.

// 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 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 throws an exception if the requested key is
// not in the list.
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 list.
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 list.
Try
    Console.WriteLine("For key = ""tif"", value = {0}.", _
        openWith("tif"))
Catch 
    Console.WriteLine("Key = ""tif"" is not found.")
End Try
// The indexer throws an exception if the requested key is
// not in the list.
try
    printfn $"""For key = "tif", value = {openWith["tif"]}."""
with 
    | :? KeyNotFoundException ->
        printfn "Key = \"tif\" is not found."
// When a program often has to try keys that turn out not to
// be in the list, 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 list, 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 list, 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
// When a program often has to try keys that turn out not to
// be in the list, TryGetValue can be a more efficient
// way to retrieve values.
match openWith.TryGetValue("tif") with
| true, value ->
    printfn "For key = \"tif\", value = {value}."
| false, _ ->
    printfn "Key = \"tif\" is not found."

Poznámky

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

Klíč nemůže být null, ale hodnota může být, pokud typ hodnot v seznamu TValueje typ odkazu.

Pokud se klíč při načítání hodnoty nenajde, KeyNotFoundException vyvolá se. Pokud se klíč při nastavování hodnoty nenajde, přidají se klíč a hodnota.

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

Jazyk C# používá this klíčové slovo k definování indexerů místo implementace Item[] vlastnosti. Visual Basic se implementuje 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 objektu 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é