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

Tanım

Belirtilen anahtarla ilişkili değeri 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ğı değerin anahtarı.

Özellik Değeri

TValue

Belirtilen anahtarla ilişkili değer. Belirtilen anahtar bulunamazsa, get işlemi bir KeyNotFoundExceptionoluşturur ve bir küme işlemi belirtilen anahtarla yeni bir öğe oluşturur.

Uygulamalar

Özel durumlar

key, null değeridir.

özelliği alınır ve key koleksiyonda yok.

Ö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 nasıl daha verimli bir yol olarak kullanılacağını gösterir.

Bu kod örneği, sınıfı için Dictionary<TKey,TValue> sağlanan daha büyük bir örneğin parçasıdır. openWith , bu örnekte kullanılan Sözlüğün adıdır.

// Create a new dictionary of strings, with string keys.
//
Dictionary<String^, String^>^ openWith =
    gcnew Dictionary<String^, String^>();

// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
openWith->Add("txt", "notepad.exe");
openWith->Add("bmp", "paint.exe");
openWith->Add("dib", "paint.exe");
openWith->Add("rtf", "wordpad.exe");

// The Add method throws an exception if the new key is
// already in the dictionary.
try
{
    openWith->Add("txt", "winword.exe");
}
catch (ArgumentException^)
{
    Console::WriteLine("An element with Key = \"txt\" already exists.");
}
// Create a new dictionary of strings, with string keys.
//
Dictionary<string, string> openWith =
    new Dictionary<string, string>();

// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");

// The Add method throws an exception if the new key is
// already in the dictionary.
try
{
    openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}
' Create a new dictionary of strings, with string keys.
'
Dim openWith As New Dictionary(Of String, String)

' Add some elements to the dictionary. There are no 
' duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")

' The Add method throws an exception if the new key is 
' already in the dictionary.
Try
    openWith.Add("txt", "winword.exe")
Catch 
    Console.WriteLine("An element with Key = ""txt"" already exists.")
End Try
// 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 C# söz dizimini kullanarak koleksiyondaki belirli bir öğeye erişme olanağı sağlar: myCollection[key] (myCollection(key) Visual Basic'te).

özelliğini, içinde bulunmayan Dictionary<TKey,TValue>bir anahtarın değerini ayarlayarak yeni öğeler eklemek için de kullanabilirsinizItem[]. Özellik değerini ayarladığınızda, anahtar içindeyse Dictionary<TKey,TValue>, bu anahtarla ilişkili değer atanan değerle değiştirilir. Anahtar içinde Dictionary<TKey,TValue>değilse, anahtar ve değer sözlüğüne eklenir. Buna karşılık, Add yöntemi mevcut öğeleri değiştirmez.

Anahtar olamaz null, ancak değer türü bir başvuru türüyse TValue bir değer olabilir.

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

Bu özelliğin değerini almak veya ayarlamak bir O(1) işlemine yaklaşır.

Şunlara uygulanır

Ayrıca bkz.