共用方式為


SortedDictionary<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 的類型無法指派給 keyTKey 索引鍵類型,則為 SortedDictionary<TKey,TValue>

實作

例外狀況

keynull

正在指定值,而且 key 是無法指派給 SortedDictionary<TKey,TValue>TKey 索引鍵型別的型別。

-或-

正在指定值,而且 value 是無法指派給 SortedDictionary<TKey,TValue>TValue 實值型別的型別。

範例

下列程式代碼範例示範如何使用 IDictionary.Item[] 屬性 (C#) System.Collections.IDictionarySortedDictionary<TKey,TValue>介面中的索引器,以及 屬性與 屬性的差異 SortedDictionary<TKey,TValue>.Item[]

此範例顯示,就像 SortedDictionary<TKey,TValue>.Item[] 屬性一樣,屬性可以變更與現有索引鍵相關聯的值,而且如果指定的索引鍵不在字典中, SortedDictionary<TKey,TValue>.IDictionary.Item[] 則可以用來新增索引鍵/值組。 此範例也會顯示與 屬性不同的SortedDictionary<TKey,TValue>.Item[]是,如果 key 不是在字典中,SortedDictionary<TKey,TValue>.IDictionary.Item[]則屬性不會擲回例外狀況,而是傳回 Null 參考。 最後,此範例示範如果 key 不是正確的數據類型,取得 SortedDictionary<TKey,TValue>.IDictionary.Item[] 屬性會傳回 Null 參考,而且如果 key 不是正確的數據類型,則設定屬性會擲回例外狀況。

程式代碼範例是較大範例的一部分,包括針對 IDictionary.Add 方法提供的輸出。

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

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

        // Add some elements to the dictionary. 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 dictionary of strings, with string keys,
        ' and access it using the IDictionary interface.
        '
        Dim openWith As IDictionary = _
            New SortedDictionary(Of String, String)
        
        ' Add some elements to the dictionary. 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 Dictionary class
// itself, IDictionary.Item does not throw an exception
// if the requested key is not in the dictionary.
Console.WriteLine("For key = \"tif\", value = {0}.",
    openWith["tif"]);
' Unlike the default Item property on the Dictionary class
' itself, IDictionary.Item does not throw an exception
' if the requested key is not in the dictionary.
Console.WriteLine("For key = ""tif"", value = {0}.", _
    openWith("tif"))
    }
}

    End Sub

End Class

備註

此屬性提供使用下列 C# 語法存取集合中特定元素的能力: myCollection[key] 在 Visual Basic 中 (myCollection(key)) 。

您也可以使用 Item[] 屬性來新增專案,方法是設定字典中不存在的索引鍵值,例如 。 myCollection["myNonexistentKey"] = myValue 不過,如果指定的索引鍵已存在於字典中,則設定 Item[] 屬性會覆寫舊的值。 相反地, IDictionary.Add 方法不會修改現有的專案。

C# 語言會使用此 關鍵詞來定義索引器,而不是實作 IDictionary.Item[] 屬性。 Visual Basic 會將 IDictionary.Item[] 實作為預設屬性,這樣會提供相同的索引功能。

取得此屬性的值是 O (記錄 n) 作業;設定屬性也是 O (記錄 n) 作業。

適用於

另請參閱