語言

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

具有指定鍵的元素。

例外狀況

key 是 null。

該屬性被檢索,但 key 找不到。

屬性為設定,且為 IDictionary<TKey,TValue> 唯讀。

範例

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

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

這些程式碼是更大範例的一部分,可以編譯並執行。 參見 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 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

備註

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

你也可以使用 Item[] 屬性來新增元素,方法是設定字典中不存在的鍵值;例如,C# 中的 myCollection["myNonexistentKey"] = myValue(Visual Basic 中的 myCollection("myNonexistentKey") = myValue)。 然而,如果指定的鍵已經存在於字典中,設定該 Item[] 屬性會覆蓋舊值。 相較之下,該 Add 方法不會修改現有元素。

實作方式在決定物件相等的方式上可能有所不同;例如,類別 List<T> 使用 Comparer<T>.Default,而類別 Dictionary<TKey,TValue> 則允許使用者指定 IComparer<T> 用於比較鍵的實作。

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

實作方式可能有所不同,是否允許 key 。null

適用於

另請參閱