語言

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中,則具有指定鍵的keykey元素或屬於無法指派於鍵型TKeySortedDictionary<TKey,TValue>別的元素。

實作

例外狀況

key 是 null。

一個值被指派,且key該值的類型無法指派到該TKey鍵型SortedDictionary<TKey,TValue>別。

-或-

一個值被指派,且其型別無法指派到該TValue值型SortedDictionary<TKey,TValue>別。

範例

以下程式碼範例說明如何將介面的IDictionary.Item[]屬性(C# 中的索引器)與 System.Collections.IDictionary ,SortedDictionary<TKey,TValue>以及該屬性與屬性的差異SortedDictionary<TKey,TValue>.Item[]。

範例顯示,與該 SortedDictionary<TKey,TValue>.Item[] 屬性類似, SortedDictionary<TKey,TValue>.IDictionary.Item[] 該屬性可以改變與現有鍵值相關的值,且若指定的鍵不在字典中,該屬性可用來新增鍵值對。 範例也顯示,與屬性 SortedDictionary<TKey,TValue>.Item[] 不同, SortedDictionary<TKey,TValue>.IDictionary.Item[] 該屬性若 key 不在字典中,則不會拋出例外,而是回傳一個空參考。 最後,範例說明 SortedDictionary<TKey,TValue>.IDictionary.Item[] 取得該屬性時,如果 key 不是正確的資料型別,則會回傳空參考;設定該屬性時,如果 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# 語言使用 this 關鍵字來定義索引器,而非實作該 IDictionary.Item[] 屬性。 Visual Basic 作為 IDictionary.Item[] 預設屬性實作,提供相同的索引功能。

取得此性質的值需操作 O(log n) ;設定該性質也需操作 O(log n) 操作。

適用於

另請參閱