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

定义

获取或设置具有指定键的元素。Gets or sets the element with the specified key.

property System::Object ^ System::Collections::IDictionary::Item[System::Object ^] { System::Object ^ get(System::Object ^ key); void set(System::Object ^ key, System::Object ^ value); };
object System.Collections.IDictionary.Item[object key] { get; set; }
 Property Item(key As Object) As Object Implements IDictionary.Item

参数

key
Object

要获取或设置的元素的键。The key of the element to get or set.

属性值

具有指定键的元素,如果 key 不在词典中或者 key 属于不能分配给 SortedList<TKey,TValue> 的键类型 TKey 的类型,则为 nullThe element with the specified key, or null if key is not in the dictionary or key is of a type that is not assignable to the key type TKey of the SortedList<TKey,TValue>.

实现

异常

keynullkey is null.

正在分配值,并且 key 属于不能分配给 SortedList<TKey,TValue> 的键类型 TKey 的类型。A value is being assigned, and key is of a type that is not assignable to the key type TKey of the SortedList<TKey,TValue>.

- 或 --or- 正在分配值,并且 value 属于不能分配给 SortedList<TKey,TValue> 的值类型 TValue 的类型。A value is being assigned, and value is of a type that is not assignable to the value type TValue of the SortedList<TKey,TValue>.

示例

下面的代码示例演示如何将IDictionary.Item[Object] System.Collections.IDictionary接口SortedList<TKey,TValue>的属性(中C#的索引器)用于和属性不同于SortedList<TKey,TValue>.Item[TKey]属性的方式。The following code example shows how to use the IDictionary.Item[Object] property (the indexer in C#) of the System.Collections.IDictionary interface with a SortedList<TKey,TValue>, and ways the property differs from the SortedList<TKey,TValue>.Item[TKey] property.

该示例显示,与SortedList<TKey,TValue>.Item[TKey]属性一样SortedList<TKey,TValue>.IDictionary.Item[Object] ,属性可以更改与现有键关联的值,并且可用于在指定键不在排序列表中时添加新的键/值对。The example shows that, like the SortedList<TKey,TValue>.Item[TKey] property, the SortedList<TKey,TValue>.IDictionary.Item[Object] property can change the value associated with an existing key and can be used to add a new key/value pair if the specified key is not in the sorted list. 该示例还显示了与SortedList<TKey,TValue>.Item[TKey]属性不同的SortedList<TKey,TValue>.IDictionary.Item[Object] ,如果key不在排序列表中,属性不会引发异常,而是返回空引用。The example also shows that unlike the SortedList<TKey,TValue>.Item[TKey] property, the SortedList<TKey,TValue>.IDictionary.Item[Object] property does not throw an exception if key is not in the sorted list, returning a null reference instead. 最后,该示例演示如果SortedList<TKey,TValue>.IDictionary.Item[Object] key不是正确的数据类型,获取属性将返回空引用; 如果key不是正确的数据类型,则设置属性会引发异常。Finally, the example demonstrates that getting the SortedList<TKey,TValue>.IDictionary.Item[Object] property returns a null reference if key is not the correct data type, and that setting the property throws an exception if key is not the correct data type.

此代码示例摘自为IDictionary.Add方法提供的更大示例,包括输出。The code example is part of a larger example, including output, provided for the IDictionary.Add method.

using System;
using System.Collections;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new sorted list of strings, with string keys,
        // and access it using the IDictionary interface.
        //
        IDictionary openWith = new SortedList<string, string>();

        // Add some elements to the sorted list. There are no 
        // duplicate keys, but some of the values are duplicates.
        // IDictionary.Add throws an exception if incorrect types
        // are supplied for key or value.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("dib", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");
Imports System.Collections
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new sorted list of strings, with string keys,
        ' and access it using the IDictionary interface.
        '
        Dim openWith As IDictionary = _
            New sortedList(Of String, String)
        
        ' Add some elements to the sorted list. There are no 
        ' duplicate keys, but some of the values are duplicates.
        ' IDictionary.Add throws an exception if incorrect types
        ' are supplied for key or value.
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("dib", "paint.exe")
        openWith.Add("rtf", "wordpad.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 indexer returns null if the key is of the wrong data 
// type.
Console.WriteLine("The indexer returns null" 
    + " if the key is of the wrong type:");
Console.WriteLine("For key = 2, value = {0}.", 
    openWith[2]);

// The indexer throws an exception when setting a value
// if the key is of the wrong data type.
try
{
    openWith[2] = "This does not get added.";
}
catch (ArgumentException)
{
    Console.WriteLine("A key of the wrong type was specified" 
        + " when assigning to the indexer.");
}
' 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 default Item property returns Nothing if the key
' is of the wrong data type.
Console.WriteLine("The default Item property returns Nothing" _
    & " if the key is of the wrong type:")
Console.WriteLine("For key = 2, value = {0}.", _
    openWith(2))

' The default Item property throws an exception when setting
' a value if the key is of the wrong data type.
Try
    openWith(2) = "This does not get added."
Catch 
    Console.WriteLine("A key of the wrong type was specified" _
        & " when setting the default Item property.")
End Try
// Unlike the default Item property on the SorteList class
// itself, IDictionary.Item does not throw an exception
// if the requested key is not in the sorted list.
Console.WriteLine("For key = \"tif\", value = {0}.", 
    openWith["tif"]);
' Unlike the default Item property on the SortedList class
' itself, IDictionary.Item does not throw an exception
' if the requested key is not in the sorted list.
Console.WriteLine("For key = ""tif"", value = {0}.", _
    openWith("tif"))
    }
}

    End Sub

End Class

注解

如果null TKey SortedList<TKey,TValue>的类型不能分配给的键类型,则此属性返回。 keyThis property returns null if key is of a type that is not assignable to the key type TKey of the SortedList<TKey,TValue>.

可以使用下面的语法通过此属性访问集合中的特定元素:myCollection[key]This property provides the ability to access a specific element in the collection by using the following syntax: myCollection[key].

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

该C#语言使用this关键字来定义索引器,而不是实现IDictionary.Item[Object]属性。The C# language uses the this keyword to define the indexers instead of implementing the IDictionary.Item[Object] property. Visual Basic 将 IDictionary.Item[Object] 实现为默认属性,该属性提供相同的索引功能。Visual Basic implements IDictionary.Item[Object] 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).

适用于

另请参阅