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

Değeri alınacak veya ayarlanacağı anahtar.

Özellik Değeri

TValue

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

Uygulamalar

Özel durumlar

key, null'e eşittir.

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

Örnekler

Aşağıdaki kod örneği, değerleri almak için özelliğini (C#'deki dizin oluşturucu) kullanır Item[] , istenen bir 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ıralı listede olmayan anahtar değerleri denemesi gerekiyorsa, değerleri almak için yöntemin nasıl daha verimli bir yol olarak kullanılacağını TryGetValue da gösterir.

Bu kod örneği, sınıfı için SortedList<TKey,TValue> sağlanan daha büyük bir örneğin parçasıdır.

// 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 Item property is another name for the indexer, so you
// can omit its name when accessing elements.
printfn $"""For key = "rtf", value = {openWith["rtf"]}."""

// The indexer can be used to change the value associated
// with a key.
openWith["rtf"] <- "winword.exe"
printfn $"""For key = "rtf", value = {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 indexer throws an exception if the requested key is
// not in the list.
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 list.
Try
    Console.WriteLine("For key = ""tif"", value = {0}.", _
        openWith("tif"))
Catch 
    Console.WriteLine("Key = ""tif"" is not found.")
End Try
// The indexer throws an exception if the requested key is
// not in the list.
try
    printfn $"""For key = "tif", value = {openWith["tif"]}."""
with 
    | :? KeyNotFoundException ->
        printfn "Key = \"tif\" is not found."
// When a program often has to try keys that turn out not to
// be in the list, 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 list, 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
// When a program often has to try keys that turn out not to
// be in the list, TryGetValue can be a more efficient
// way to retrieve values.
match openWith.TryGetValue("tif") with
| true, value ->
    printfn "For key = \"tif\", value = {value}."
| false, _ ->
    printfn "Key = \"tif\" is not found."

Açıklamalar

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

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

Bir değer alınırken anahtar bulunamazsa, KeyNotFoundException oluşturulur. Bir değer ayarlanırken anahtar bulunamazsa anahtar ve değer eklenir.

özelliğini, içinde bulunmayan Item[]bir anahtarın değerini ayarlayarak yeni öğeler eklemek için de kullanabilirsinizSortedList<TKey,TValue>; örneğin, myCollection["myNonexistentKey"] = myValue. Ancak, belirtilen anahtar içinde SortedList<TKey,TValue>zaten varsa, özelliğini ayarlamak Item[] eski değerin üzerine yazar. Buna karşılık, Add yöntemi mevcut öğeleri değiştirmez.

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

Bu özelliğin değerinin alınması bir O(log n) işlemidir; burada n değeri olur Count. anahtarı zaten içindeyse nözelliğini ayarlamak bir O(log SortedList<TKey,TValue>) işlemidir. Anahtar listede değilse, özelliği ayarlamak sıralanmamış veriler için bir O(n) işlemi veya listenin sonuna yeni öğe ekleniyorsa O(log n) işlemidir. Ekleme yeniden boyutlandırmaya neden oluyorsa işlem O()n olur.

Şunlara uygulanır

Ayrıca bkz.