IDictionary<TKey,TValue>.Item[TKey] Properti

Definisi

Mendapatkan atau mengatur elemen dengan kunci yang ditentukan.

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

Parameter

key
TKey

Kunci elemen untuk mendapatkan atau mengatur.

Nilai Properti

TValue

Elemen dengan kunci yang ditentukan.

Pengecualian

keyadalah null.

Properti diambil dan key tidak ditemukan.

Properti diatur dan IDictionary<TKey,TValue> bersifat baca-saja.

Contoh

Contoh kode berikut menggunakan Item[] properti (pengindeks di C#) untuk mengambil nilai, menunjukkan bahwa kunci yang KeyNotFoundException diminta tidak ada, dan menunjukkan bahwa nilai yang terkait dengan kunci dapat diganti.

Contoh ini juga menunjukkan cara menggunakan TryGetValue metode sebagai cara yang lebih efisien untuk mengambil nilai jika program sering harus mencoba nilai kunci yang tidak ada dalam kamus.

Kode ini adalah bagian dari contoh yang lebih besar yang dapat dikompilasi dan dijalankan. Lihat 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

Keterangan

Properti ini menyediakan kemampuan untuk mengakses elemen tertentu dalam koleksi dengan menggunakan sintaks berikut: myCollection[key] (myCollection(key) dalam Visual Basic).

Anda juga dapat menggunakan Item[] properti untuk menambahkan elemen baru dengan mengatur nilai kunci yang tidak ada dalam kamus; misalnya, myCollection["myNonexistentKey"] = myValue di C# (myCollection("myNonexistentKey") = myValue di Visual Basic). Namun, jika kunci yang ditentukan sudah ada dalam kamus, mengatur Item[] properti akan menimpa nilai lama. Sebaliknya, Add metode tidak memodifikasi elemen yang ada.

Implementasi dapat bervariasi dalam cara menentukan kesetaraan objek; misalnya, List<T> kelas menggunakan Comparer<T>.Default, sedangkan Dictionary<TKey,TValue> kelas memungkinkan pengguna untuk menentukan implementasi yang IComparer<T> akan digunakan untuk membandingkan kunci.

Bahasa C# menggunakan kata kunci ini untuk menentukan pengindeks alih-alih mengimplementasikan Item[] properti . Visual Basic mengimplementasikan Item[] sebagai properti default, yang menyediakan fungsionalitas pengindeksan yang sama.

Implementasi dapat bervariasi dalam apakah memungkinkan key untuk menjadi null.

Berlaku untuk

Lihat juga