IDictionary<TKey,TValue>.Item[TKey] Właściwość

Definicja

Pobiera lub ustawia element przy użyciu określonego klucza.

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

Klucz elementu do pobrania lub ustawienia.

Wartość właściwości

TValue

Element z określonym kluczem.

Wyjątki

key to null.

Właściwość jest pobierana i key nie została znaleziona.

Właściwość jest ustawiona i IDictionary<TKey,TValue> jest tylko do odczytu.

Przykłady

Poniższy przykład kodu używa Item[] właściwości (indeksatora w języku C#) do pobierania wartości, co pokazuje, że element KeyNotFoundException jest zgłaszany, gdy żądany klucz nie jest obecny, i pokazuje, że wartość skojarzona z kluczem może zostać zamieniona.

W przykładzie pokazano również, jak używać TryGetValue metody jako bardziej wydajnego sposobu pobierania wartości, jeśli program często musi wypróbować wartości kluczy, które nie znajdują się w słowniku.

Ten kod jest częścią większego przykładu, który można skompilować i wykonać. Zobacz: .

// 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

Uwagi

Ta właściwość umożliwia dostęp do określonego elementu w kolekcji przy użyciu następującej składni: myCollection[key] (myCollection(key) w Visual Basic).

Możesz również użyć Item[] właściwości , aby dodać nowe elementy, ustawiając wartość klucza, który nie istnieje w słowniku, na przykład myCollection["myNonexistentKey"] = myValue w języku C# (myCollection("myNonexistentKey") = myValue w Visual Basic). Jeśli jednak określony klucz już istnieje w słowniku, ustawienie Item[] właściwości zastępuje starą wartość. Add Natomiast metoda nie modyfikuje istniejących elementów.

Implementacje mogą różnić się w sposobie określania równości obiektów; na przykład List<T> klasa używa klasy Comparer<T>.Default, natomiast Dictionary<TKey,TValue> klasa umożliwia użytkownikowi określenie IComparer<T> implementacji do użycia do porównywania kluczy.

Język C# używa tego słowa kluczowego do zdefiniowania indeksatorów zamiast implementowania Item[] właściwości. Visual Basic implementuje Item[] jako właściwość domyślną, która zapewnia tę samą funkcjonalność indeksowania.

Implementacje mogą się różnić w zależności od tego, czy zezwalają key na wartość null.

Dotyczy

Zobacz też