Dictionary<TKey,TValue>.IDictionary.Item[Object] Dictionary<TKey,TValue>.IDictionary.Item[Object] Dictionary<TKey,TValue>.IDictionary.Item[Object] Property

定義

指定したキーの値を取得または設定します。Gets or sets the value 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; }
 Property Item(key As Object) As Object Implements IDictionary.Item

パラメーター

key
Object Object Object

取得する値のキー。The key of the value to get.

プロパティ値

指定したキーに関連付けられた値。ただし、key がディクショナリにない場合、または keyDictionary<TKey,TValue> のキー型 TKey に割り当てられない型である場合は nullThe value associated 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 Dictionary<TKey,TValue>.

実装

例外

値が割り当てられているとkeyがキー型に割り当てることはできません型のTKeyDictionary<TKey,TValue>します。A value is being assigned, and key is of a type that is not assignable to the key type TKey of the Dictionary<TKey,TValue>.

- または --or- 値が割り当てられているとvalueが値の型に割り当てることはできません型のTValueDictionary<TKey,TValue>します。A value is being assigned, and value is of a type that is not assignable to the value type TValue of the Dictionary<TKey,TValue>.

次のコード例を使用する方法を示しています、IDictionary.Item[Object]のプロパティ (c# のインデクサー)、System.Collections.IDictionaryとのインターフェイスをDictionary<TKey,TValue>、および方法は、プロパティとは異なります、Dictionary<TKey,TValue>.Item[TKey]プロパティ。The following code example shows how to use the IDictionary.Item[Object] property (the indexer in C#) of the System.Collections.IDictionary interface with a Dictionary<TKey,TValue>, and ways the property differs from the Dictionary<TKey,TValue>.Item[TKey] property.

などの例を示します、Dictionary<TKey,TValue>.Item[TKey]プロパティ、Dictionary<TKey,TValue>.IDictionary.Item[Object]プロパティは、既存のキーに関連付けられている値を変更することができ、指定したキーがディクショナリにない場合は、新しいキー/値ペアを追加するために使用できます。The example shows that, like the Dictionary<TKey,TValue>.Item[TKey] property, the Dictionary<TKey,TValue>.IDictionary.Item[Object] 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 dictionary. 例とは異なり、表示も、Dictionary<TKey,TValue>.Item[TKey]プロパティ、Dictionary<TKey,TValue>.IDictionary.Item[Object]プロパティが例外をスローしていない場合keyがディクショナリに返されない、null 参照代わりにします。The example also shows that unlike the Dictionary<TKey,TValue>.Item[TKey] property, the Dictionary<TKey,TValue>.IDictionary.Item[Object] property does not throw an exception if key is not in the dictionary, returning a null reference instead. 最後にの例ではその取得を示しています、Dictionary<TKey,TValue>.IDictionary.Item[Object]場合、プロパティが null 参照を返しますkey、正しいデータ型でないし、例外をスロー プロパティを設定key正しいデータ型ではありません。Finally, the example demonstrates that getting the Dictionary<TKey,TValue>.IDictionary.Item[Object] 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.Addメソッド。The 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 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
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 で)。This property provides the ability to access a specific value in the collection by using the following C# syntax: myCollection[key] (myCollection(key) in Visual Basic).

使用することも、Item[Object]ディクショナリ内に存在しないキーの値を設定して、新しい要素を追加するプロパティなど、myCollection["myNonexistentKey"] = myValueします。You can also use the Item[Object] 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[Object]プロパティには、古い値が上書きされます。However, if the specified key already exists in the dictionary, setting the Item[Object] property overwrites the old value. これに対し、Addメソッドでは、既存の要素は変更しません。In contrast, the Add method does not modify existing elements.

C# 言語を使用して、このキーワードを実装する代わりに、インデクサーの定義、IDictionary.Item[Object]プロパティ。The C# language uses the this keyword to define the indexers instead of implementing the IDictionary.Item[Object] property. Visual Basic は、IDictionary.Item[Object] を既定のプロパティとして実装しており、同様のインデックス機能を提供します。Visual Basic implements IDictionary.Item[Object] as a default property, which provides the same indexing functionality.

このプロパティの値の設定を取得または o (1) 操作に近くなります。Getting or setting the value of this property approaches an O(1) operation.

適用対象

こちらもご覧ください