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

定義

取得或設定具有指定索引鍵的項目。Gets or sets the element with the specified key.

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

要取得或設定之項目的索引鍵。The key of the element to get or set.

屬性值

TValue

具有指定索引鍵的項目。The element with the specified key.

例外狀況

keynullkey is null.

屬性已擷取,並且找不到 keyThe property is retrieved and key is not found.

屬性已設定,而且 IDictionary<TKey,TValue> 是唯讀的。The property is set and the IDictionary<TKey,TValue> is read-only.

範例

下列程式碼範例會使用 Item[TKey] 屬性(中C#的索引子)來抓取值,示範當要求的索引鍵不存在時,會擲回 KeyNotFoundException,並顯示可以取代與索引鍵相關聯的值。The following code example uses the Item[TKey] property (the indexer in C#) to retrieve values, demonstrating that a KeyNotFoundException is thrown when a requested key is not present, and showing that the value associated with a key can be replaced.

此範例也會示範如何使用 TryGetValue 方法,以便在程式經常必須嘗試不在字典中的索引鍵值時,更有效率地取得值。The example also shows how to use the TryGetValue method as a more efficient way to retrieve values if a program often must try key values that are not in the dictionary.

此程式碼是可編譯和執行之較大範例的一部分。This code is part of a larger example that can be compiled and executed. 請參閱System.Collections.Generic.IDictionary<TKey,TValue>.See 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 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 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", 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.
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))。This property provides the ability to access a specific element in the collection by using the following syntax: myCollection[key] (myCollection(key) in Visual Basic).

您也可以藉由設定不存在於字典中之索引鍵的值,使用 Item[TKey] 屬性來加入新的元素。例如,中C#的 myCollection["myNonexistentKey"] = myValue (Visual Basic 中的myCollection("myNonexistentKey") = myValue)。You can also use the Item[TKey] property to add new elements by setting the value of a key that does not exist in the dictionary; for example, myCollection["myNonexistentKey"] = myValue in C# (myCollection("myNonexistentKey") = myValue in Visual Basic). 不過,如果指定的索引鍵已存在於字典中,則設定 Item[TKey] 屬性會覆寫舊值。However, if the specified key already exists in the dictionary, setting the Item[TKey] property overwrites the old value. 相反地,Add 方法不會修改現有的元素。In contrast, the Add method does not modify existing elements.

在決定物件是否相等的情況中,可能會有不同的執行方式。例如,List<T> 類別使用 Comparer<T>.Default,而 Dictionary<TKey,TValue> 類別則可讓使用者指定要用來比較索引鍵的 IComparer<T> 執行。Implementations can vary in how they determine equality of objects; for example, the List<T> class uses Comparer<T>.Default, whereas the Dictionary<TKey,TValue> class allows the user to specify the IComparer<T> implementation to use for comparing keys.

C#語言會使用this關鍵字來定義索引子,而不是執行 Item[TKey] 屬性。The C# language uses the this keyword to define the indexers instead of implementing the Item[TKey] property. Visual Basic 會將 Item[TKey] 實作為預設屬性,這樣會提供相同的索引功能。Visual Basic implements Item[TKey] as a default property, which provides the same indexing functionality.

不論是否允許 null``key,都可能會有不同的執行方式。Implementations can vary in whether they allow key to be null.

適用於

另請參閱