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 がディクショナリに存在しない場合、または keykey のキーの型 TKey に代入できない型である場合は SortedList<TKey,TValue>

実装

例外

keynullです。

値を代入しようとしていますが、key は、SortedList<TKey,TValue> のキーの型 TKey に代入できない型です。

または

値を代入しようとしていますが、value は、SortedList<TKey,TValue> の値の型 TValue に代入できない型です。

次のコード例は、 インターフェイスSortedList<TKey,TValue>IDictionary.Item[] プロパティ (C#のインデクサー) System.Collections.IDictionary を 使用する方法と、 プロパティが プロパティと異なる方法をSortedList<TKey,TValue>.Item[]示しています。

この例では、 プロパティと同様に SortedList<TKey,TValue>.Item[] 、 プロパティは既存のキーに関連付けられている値を変更でき、 SortedList<TKey,TValue>.IDictionary.Item[] 指定したキーが並べ替えられた一覧にない場合は、新しいキーと値のペアを追加するために使用できることを示しています。 また、この例では、 プロパティとは異なり SortedList<TKey,TValue>.Item[]SortedList<TKey,TValue>.IDictionary.Item[] が並べ替えられたリストにない場合 key 、プロパティは例外をスローせず、代わりに null 参照を返すことも示しています。 最後に、この例では、 が正しいデータ型でない場合keyに プロパティをSortedList<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 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

注釈

のキーTKey型に割り当てられない型の が の場合key、このプロパティは を返nullしますSortedList<TKey,TValue>

このプロパティは、構文 myCollection[key] を使用して、コレクションの特定の要素にアクセスする機能を提供します。

プロパティを Item[] 使用して、ディクショナリに存在しないキーの値 (例: myCollection["myNonexistentKey"] = myValue) を設定して、新しい要素を追加することもできます。 ただし、指定したキーがディクショナリに既に存在する場合は、 プロパティを Item[] 設定すると古い値が上書きされます。 これに対し、 メソッドは既存の Add 要素を変更しません。

C# 言語では、 プロパティを実装する代わりに、 この キーワードを使用してインデクサーを IDictionary.Item[] 定義します。 Visual Basic は、IDictionary.Item[] を既定のプロパティとして実装しており、同様のインデックス機能を提供します。

このプロパティの値を取得するのは O(log n) 操作です。ここで、n は です Count。 キーが に既SortedList<TKey,TValue>に存在する場合、プロパティの設定は O(log n) 操作です。 キーがリストにない場合、プロパティの設定は、並べ替えられていないデータに対する O(n) 操作、またはリストの末尾に新しい要素が追加された場合は O(log n) です。 挿入によってサイズが変更された場合、操作は O(n) になります。

適用対象

こちらもご覧ください