IDictionary<TKey,TValue>.Item[TKey] IDictionary<TKey,TValue>.Item[TKey] IDictionary<TKey,TValue>.Item[TKey] IDictionary<TKey,TValue>.Item[TKey] Property

定義

取得或設定具有指定索引鍵的項目。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 TKey TKey TKey

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

屬性值

TValue TValue TValue TValue

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

例外狀況

屬性已擷取,並且找不到 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"] = myValueC# 中 (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.

使用 C# 語言關鍵字來定義索引,而不必實作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.

實作有所不同是否允許keynullImplementations can vary in whether they allow key to be null.

適用於

另請參閱