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

定义

获取或设置具有指定键的元素。

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; }
object? System.Collections.IDictionary.Item[object key] { get; set; }
member this.System.Collections.IDictionary.Item(obj) : obj with get, set
 Property Item(key As Object) As Object Implements IDictionary.Item

参数

key
Object

要获取或设置的元素的键。

属性值

如果 null 不在字典中或 key 的类型属于不能分配给 key 的键类型 TKey,则为具有指定键的元素或 SortedList<TKey,TValue>

实现

例外

keynull

正在分配值,并且 key 属于不能分配给 SortedList<TKey,TValue> 的键类型 TKey 的类型。

正在分配值,并且 value 属于不能分配给 SortedList<TKey,TValue> 的值类型 TValue 的类型。

示例

下面的代码示例演示如何将 IDictionary.Item[] 属性 (C#) 接口的 System.Collections.IDictionary 索引器中, SortedList<TKey,TValue>以及 属性与 属性的不同 SortedList<TKey,TValue>.Item[] 之处。

该示例显示,与 属性一样 SortedList<TKey,TValue>.Item[]SortedList<TKey,TValue>.IDictionary.Item[] 属性可以更改与现有键关联的值,并且如果指定的键不在排序列表中,则可用于添加新的键/值对。 该示例还表明,与 属性不同 SortedList<TKey,TValue>.Item[]SortedList<TKey,TValue>.IDictionary.Item[] 如果 key 不在排序列表中,则 属性不会引发异常,而是返回 null 引用。 最后,该示例演示,如果不是正确的数据类型, SortedList<TKey,TValue>.IDictionary.Item[] 则获取属性将返回 null 引用;如果 keykey 不是正确的数据类型,则设置属性将引发异常。

代码示例是为 方法提供的更大示例(包括输出)的一 IDictionary.Add 部分。

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

注解

如果 key 的类型不可分配给 的键类型SortedList<TKey,TValue>TKey,则此属性返回 null

通过此属性,可以使用以下语法来访问集合中的特定元素:myCollection[key]

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

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

检索此属性的值是 O (日志 n) 操作,其中 n 为 Count。 设置 属性是 O (日志 n) 操作(如果 键已位于 中 SortedList<TKey,TValue>)。 如果键不在列表中,则设置 属性是未排序数据的 O (n) 操作;如果在列表末尾添加新元素,则 O (日志 n) 。 如果插入导致重设大小,则操作为 O (n) 。

适用于

另请参阅