Dictionary<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

取得する値のキー。

プロパティ値

Object

指定したキーに関連付けられた値。ただし、key がディクショナリにない場合、または keyDictionary<TKey,TValue> のキー型 TKey に割り当てられない型である場合は null

実装

例外

keynullです。

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

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

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

この例では、プロパティと同様に Dictionary<TKey,TValue>.Item[] 、プロパティは既存のキーに関連付けられている値を変更でき、 Dictionary<TKey,TValue>.IDictionary.Item[] 指定したキーがディクショナリにない場合は、新しいキーと値のペアを追加するために使用できます。 この例では、プロパティとは異なり Dictionary<TKey,TValue>.Item[]Dictionary<TKey,TValue>.IDictionary.Item[] ディクショナリに含まれていない場合 key 、プロパティは例外をスローせず、代わりに null 参照を返すことも示しています。 最後に、プロパティを取得 Dictionary<TKey,TValue>.IDictionary.Item[] すると、正しいデータ型でない場合 key は null 参照が返され、正しいデータ型でない場合 key はプロパティの設定によって例外がスローされることを示します。

このコード例は、メソッドに対して提供される出力を含む、より大きな例の IDictionary.Add 一部です。

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

public class Example
{
    public static void Main()
    {
        // Create a new dictionary of strings, with string keys,
        // and access it using the IDictionary interface.
        //
        IDictionary openWith = new Dictionary<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 dictionary of strings, with string keys,
        ' and access it using the IDictionary interface.
        '
        Dim openWith As IDictionary = _
            New Dictionary(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]``myCollection(key) (Visual Basic) を使用して、コレクション内の特定の値にアクセスする機能を提供します。

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

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

このプロパティの値を取得または設定すると、O(1) 操作に近づく。

適用対象

こちらもご覧ください