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.

示例

下面的代码示例使用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.

该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.

实现在是否允许key null时可能有所不同。Implementations can vary in whether they allow key to be null.

适用于

另请参阅