Bagikan melalui


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

Definisi

Mendapatkan atau mengatur nilai yang terkait 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 nilai untuk mendapatkan atau mengatur.

Nilai Properti

TValue

Nilai yang terkait dengan kunci yang ditentukan. Jika kunci yang ditentukan tidak ditemukan, operasi get akan melempar KeyNotFoundException, dan operasi yang ditetapkan akan membuat elemen baru dengan kunci yang ditentukan.

Penerapan

Pengecualian

keyadalah null.

Properti diambil dan key tidak ada dalam koleksi.

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 kali harus mencoba nilai kunci yang tidak ada dalam kamus.

Contoh kode ini adalah bagian dari contoh yang lebih besar yang disediakan untuk SortedDictionary<TKey,TValue> kelas .

// 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 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", 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 C# 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 di SortedDictionary<TKey,TValue>; misalnya, myCollection["myNonexistentKey"] = myValue. Namun, jika kunci yang ditentukan sudah ada di SortedDictionary<TKey,TValue>, pengaturan Item[] properti akan menimpa nilai lama. Sebaliknya, Add metode tidak memodifikasi elemen yang ada.

Kunci tidak boleh null, tetapi nilainya bisa, jika jenis TValue nilai adalah jenis referensi.

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.

Mendapatkan nilai properti ini adalah operasi O(log n) ; mengatur properti juga merupakan operasi O(log n).

Berlaku untuk

Lihat juga