語言

SortedDictionary<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 操作拋出 , KeyNotFoundException集合操作則會產生帶有指定鍵的新元素。

實作

例外狀況

key 是 null。

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

範例

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

範例也展示了當程式經常必須嘗試字典中不存在的鍵值時,如何將此 TryGetValue 方法作為更有效率的取值方法。

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

備註

此特性允許使用以下 C# 語法存取集合中的特定元素:myCollection[key](在 Visual Basic 中為 myCollection(key))。

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

若值null類型是參考型態,鍵值不可能是 TValue,但值可以是。

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

取得此性質的值需操作 O(log n) ;設定該性質也需操作 O(log n) 操作。

適用於

另請參閱