SortedList<TKey,TValue>.Item[TKey] 属性

定义

获取或设置与指定的键关联的值。Gets or sets the value associated 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 whose value to get or set.

属性值

TValue

与指定的键相关联的值。The value associated with the specified key. 如果找不到指定的键,则 get 操作会引发 KeyNotFoundException,而 set 操作会创建一个使用指定键的新元素。If the specified key is not found, a get operation throws a KeyNotFoundException and a set operation creates a new element using the specified key.

实现

异常

keynullkey is null.

已检索该属性,并且集合中不存在 keyThe property is retrieved and key does not exist in the collection.

示例

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

此代码示例是为SortedList<TKey,TValue>类提供的更大示例的一部分。This code example is part of a larger example provided for the SortedList<TKey,TValue> class.

// 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 list.
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 list.
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 list.
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 list, 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 list, 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 list, 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]This property provides the ability to access a specific element in the collection by using the following syntax: myCollection[key].

键不能为null,但如果TValue列表中值的类型为引用类型,则值可以为。A key cannot be null, but a value can be, if the type of values in the list, TValue, is a reference type.

如果在检索值时找不到该键, KeyNotFoundException则会引发。If the key is not found when a value is being retrieved, KeyNotFoundException is thrown. 如果在设置值时找不到该键,则添加键和值。If the key is not found when a value is being set, the key and value are added.

你还可以使用Item[TKey]属性来添加新元素,方法是设置SortedList<TKey,TValue>中不存在的键的值; 例如myCollection["myNonexistentKey"] = myValueYou can also use the Item[TKey] property to add new elements by setting the value of a key that does not exist in the SortedList<TKey,TValue>; for example, myCollection["myNonexistentKey"] = myValue. 但是,如果指定的键已存在于中SortedList<TKey,TValue>,则Item[TKey]设置属性会覆盖旧值。However, if the specified key already exists in the SortedList<TKey,TValue>, setting the Item[TKey] property overwrites the old value. 与此Add相反,方法不会修改现有元素。In contrast, the Add method does not modify existing elements.

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

检索此属性的值是一个 O (log n)操作,其中 n 是。 CountRetrieving the value of this property is an O(log n) operation, where n is Count. 如果键已存在n SortedList<TKey,TValue>于中,则设置属性是一个 O (log)操作。Setting the property is an O(log n) operation if the key is already in the SortedList<TKey,TValue>. 如果键不在列表中,则为未排序的数据设置属性是no ()运算,如果在列表的末尾n添加新元素,则为 o (log)。If the key is not in the list, setting the property is an O(n) operation for unsorted data, or O(log n) if the new element is added at the end of the list. 如果插入操作导致大小调整,则操作为 On()。If insertion causes a resize, the operation is O(n).

适用于

另请参阅