語言

SortedList<TKey,TValue>.Item[TKey] 屬性

定義

取得或設定與指定索引鍵相關聯的值。

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

參數

key
TKey

就是要取得或設定的金鑰值。

屬性值

TValue

與指定鍵相關聯的值。 若找不到指定的鍵,則執行 get 操作拋出 a KeyNotFoundException ,並以 set 操作使用指定鍵建立新元素。

實作

例外狀況

key 是 null。

該屬性被檢索後,在 key 集合中不存在。

範例

以下程式碼範例使用 Item[] 屬性(C# 中的索引器)來取得值,示範當請求的金鑰不存在時會拋出 a KeyNotFoundException ,並說明與鍵相關的值可以被替換。

範例也展示了當程式經常需要嘗試不在排序列表中的鍵值時,如何將此 TryGetValue 方法作為更有效率的取值方法。

此程式碼範例是本類別更大範例 SortedList<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 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."

備註

此特性可透過以下語法存取集合中的特定元素: myCollection[key]。

鍵不能是 ,但如果列表null中的值類型 為參考型別,則 值可以是 TValue。

若在取值時找不到該金鑰, KeyNotFoundException 則會拋出。 如果設定值時找不到該鍵,則會將鍵與值相加。

你也可以利用這個 Item[] 屬性來新增元素,方法是設定不存在 SortedList<TKey,TValue>於 中的鍵值;例如, myCollection["myNonexistentKey"] = myValue。 然而,如果指定的鍵已經存在 SortedList<TKey,TValue>於 中,設定屬性 Item[] 會覆蓋舊值。 相較之下,該 Add 方法不會修改現有元素。

C# 語言使用 this 關鍵字來定義索引器,而非實作該 Item[] 屬性。 Visual Basic 作為 Item[] 預設屬性實作,提供相同的索引功能。

取得此性質的值需進行 O(log n) 操作,其中 n 為 Count。 若鍵已在 n中,設定屬性即為 O(log SortedList<TKey,TValue>) 操作。 若鍵不在清單中,設定屬性為 O(n) 處理未排序資料,若新增元素則為 O(log n) 操作。 若插入導致大小調整,則操作為 O(n)。

適用於

另請參閱