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; }
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

要取得或設定之項目的索引鍵。The key of the element to get or set.

屬性值

Object

具有指定索引鍵的項目;如果 null 不在字典中,或 key 的類型無法指派給 keyTKey 索引鍵類型,則為 SortedList<TKey,TValue>The 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[] 屬性, (c # 中的索引子 ) 的 System.Collections.IDictionary 介面 SortedList<TKey,TValue> ,以及屬性與屬性不同的方式 SortedList<TKey,TValue>.Item[]The following code example shows how to use the IDictionary.Item[] 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[] property.

此範例顯示,如同 SortedList<TKey,TValue>.Item[] 屬性, SortedList<TKey,TValue>.IDictionary.Item[] 屬性可以變更與現有索引鍵關聯的值,如果指定的索引鍵不在排序的清單中,則可以用來加入新的索引鍵/值組。The example shows that, like the SortedList<TKey,TValue>.Item[] property, the SortedList<TKey,TValue>.IDictionary.Item[] 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[] SortedList<TKey,TValue>.IDictionary.Item[] 是,如果不 key 在排序的清單中,屬性不會擲回例外狀況,而會改為傳回 null 參考。The example also shows that unlike the SortedList<TKey,TValue>.Item[] property, the SortedList<TKey,TValue>.IDictionary.Item[] property does not throw an exception if key is not in the sorted list, returning a null reference instead. 最後,此範例會示範如果不是 SortedList<TKey,TValue>.IDictionary.Item[] key 正確的資料類型,取得屬性會傳回 null 參考,而且如果不 key 是正確的資料類型,則設定屬性會擲回例外狀況。Finally, the example demonstrates that getting the SortedList<TKey,TValue>.IDictionary.Item[] 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.AddThe 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如果的型別 key 無法指派給的索引鍵類型,則這個屬性會傳回 TKey SortedList<TKey,TValue>This 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[] 屬性來加入新的專案,方法是設定不存在於字典中的索引鍵值; 例如 myCollection["myNonexistentKey"] = myValueYou can also use the Item[] 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[] 屬性會覆寫舊的值。However, if the specified key already exists in the dictionary, setting the Item[] property overwrites the old value. 相反地,此 Add 方法不會修改現有的元素。In contrast, the Add method does not modify existing elements.

C # 語言使用 this 關鍵字來定義索引子,而不是執行 IDictionary.Item[] 屬性。The C# language uses the this keyword to define the indexers instead of implementing the IDictionary.Item[] property. Visual Basic 會將 IDictionary.Item[] 實作為預設屬性,這樣會提供相同的索引功能。Visual Basic implements IDictionary.Item[] as a default property, which provides the same indexing functionality.

將這個屬性的值取出是 (記錄) 作業 n ,其中 n 是 CountRetrieving the value of this property is an O(log n) operation, where n is Count. 如果索引鍵已在中,則設定屬性是 O (記錄 n) 作業 SortedList<TKey,TValue>Setting the property is an O(log n) operation if the key is already in the SortedList<TKey,TValue>. 如果索引鍵不在清單中,則設定屬性是一個 O (未 n 排序資料的) 作業, n 如果在清單結尾加入新元素,則為 o (記錄) 。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. 如果插入造成調整大小,則作業 (n) 。If insertion causes a resize, the operation is O(n).

適用於

另請參閱