IDictionary<TKey,TValue>.Item[TKey] Özellik

Tanım

Belirtilen anahtarla öğesini alır veya ayarlar.

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

Parametreler

key
TKey

Alınacak veya ayarlanacağı öğenin anahtarı.

Özellik Değeri

TValue

Belirtilen anahtara sahip öğesi.

Özel durumlar

key, null değeridir.

özelliği alınır ve key bulunamaz.

özelliği ayarlanır ve IDictionary<TKey,TValue> salt okunurdur.

Örnekler

Aşağıdaki kod örneği, değerleri almak için özelliğini (C#'de dizin oluşturucu) kullanır Item[] , istenen anahtar mevcut olmadığında a'nın KeyNotFoundException atıldığını gösterir ve bir anahtarla ilişkili değerin değiştirilebileceğini gösterir.

Örnek ayrıca, bir programın genellikle sözlükte TryGetValue olmayan anahtar değerleri denemesi gerekiyorsa değerleri almak için yöntemin daha verimli bir yol olarak nasıl kullanılacağını da gösterir.

Bu kod, derlenip yürütülebilen daha büyük bir örneğin parçasıdır. Bkz. System.Collections.Generic.IDictionary<TKey,TValue>.

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

Açıklamalar

Bu özellik, aşağıdaki söz dizimini kullanarak koleksiyondaki belirli bir öğeye erişme olanağı sağlar: myCollection[key] (myCollection(key) Visual Basic'te).

Özelliğini, sözlükte Item[] bulunmayan bir anahtarın değerini ayarlayarak yeni öğeler eklemek için de kullanabilirsiniz; örneğin, myCollection["myNonexistentKey"] = myValue C# içinde (myCollection("myNonexistentKey") = myValue Visual Basic'te). Ancak, belirtilen anahtar sözlükte zaten varsa, özelliğini ayarlamak Item[] eski değerin üzerine yazar. Buna karşılık, Add yöntemi mevcut öğeleri değiştirmez.

Uygulamalar nesnelerin eşitliğini belirleme şekline göre farklılık gösterebilir; örneğin, List<T> sınıfı kullanır Comparer<T>.Default, sınıfı ise Dictionary<TKey,TValue> kullanıcının anahtarları karşılaştırmak için kullanılacak uygulamayı belirtmesine IComparer<T> izin verir.

C# dili , özelliğini uygulamak Item[] yerine dizin oluşturucuları tanımlamak için bu anahtar sözcüğü kullanır. Visual Basic, aynı dizin oluşturma işlevini sağlayan varsayılan bir özellik olarak uygular Item[] .

Uygulamalar, olmasına izin key verip vermediklerine nullgöre farklılık gösterebilir.

Şunlara uygulanır

Ayrıca bkz.