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

带有指定键的元素。

例外

keynull

检索了属性但没有找到 key

设置了该属性,但 IDictionary<TKey,TValue> 为只读。

示例

下面的代码示例使用 Item[] C#) 中索引器 (属性来检索值,演示 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 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)

还可以使用 Item[] 属性通过设置字典中不存在的键的值来添加新元素;例如, myCollection["myNonexistentKey"] = myValue 在 C# (myCollection("myNonexistentKey") = myValue Visual Basic) 。 但是,如果字典中已存在指定的键,则设置 Item[] 属性将覆盖旧值。 相反, Add 方法不修改现有元素。

实现在确定对象相等性的方式上可能会有所不同;例如, List<T> 类使用 Comparer<T>.Default,而 Dictionary<TKey,TValue> 类允许用户指定 IComparer<T> 用于比较键的实现。

C# 语言使用此关键字 (keyword) 来定义索引器,而不是实现 Item[] 属性。 Visual Basic 将 Item[] 实现为默认属性,该属性提供相同的索引功能。

实现在是否允许 keynull方面可能会有所不同。

适用于

另请参阅