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]myCollection(key)在 Visual Basic 中)。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]使用屬性來加入新專案,方法是設定不存在於字典中之索引鍵的值; 例如, myCollection["myNonexistentKey"] = myValue中C#的(myCollection("myNonexistentKey") = myValue Visual Basic)。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.

語言會使用 this 關鍵字來定義索引子,而不是執行Item[TKey]屬性。 C#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.

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

適用於

另請參閱