IDictionary.Item プロパティ
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As IDictionary Dim key As Object Dim value As Object value = instance(key) instance(key) = value
property Object^ default [Object^] { Object^ get (Object^ key); void set (Object^ key, Object^ value); }
/** @property */ Object get_Item (Object key) /** @property */ void set_Item (Object key, Object value)
プロパティ値
指定したキーを持つ要素。


このプロパティでは、myCollection[key] という構文を使用して、コレクション内の特定の要素にアクセスできます。
Item プロパティを使用すると、ディクショナリ内に存在しないキーの値を設定することで、新しい要素を追加することもできます (例 : myCollection["myNonexistentKey"] = myValue)。ただし、指定したキーがディクショナリに既に存在する場合、Item プロパティを設定すると、既存の値が上書きされます。対照的に、Add メソッドは既存の要素を変更しません。
キーを null 参照 (Visual Basic では Nothing) にできるかどうかは、実装によって異なります。

Item プロパティを実装する方法を次のコード例に示します。このコード例は、IDictionary クラスのトピックで取り上げているコード例の一部です。
Public Property Item(ByVal key As Object) As Object Implements IDictionary.Item Get ' If this key is in the dictionary, return its value. Dim index As Integer If TryGetIndexOfKey(key, index) Then ' The key was found return its value. Return items(index).Value Else ' The key was not found return null. Return Nothing End If End Get Set(ByVal value As Object) ' If this key is in the dictionary, change its value. Dim index As Integer If TryGetIndexOfKey(key, index) Then ' The key was found change its value. items(index).Value = value Else ' This key is not in the dictionary add this key/value pair. Add(key, value) End If End Set End Property
public object this[object key] { get { // If this key is in the dictionary, return its value. Int32 index; if (TryGetIndexOfKey(key, out index)) { // The key was found; return its value. return items[index].Value; } else { // The key was not found; return null. return null; } } set { // If this key is in the dictionary, change its value. Int32 index; if (TryGetIndexOfKey(key, out index)) { // The key was found; change its value. items[index].Value = value; } else { // This key is not in the dictionary; add this key/value pair. Add(key, value); } } }
public: virtual property Object^ default[Object^] { Object^ get(Object^ key) { // If this key is in the dictionary, return its value. int index; if (TryGetIndexOfKey(key, &index)) { // The key was found; return its value. return items[index]->Value; } else { // The key was not found; return null. return nullptr; } } void set(Object^ key, Object^ value) { // If this key is in the dictionary, change its value. int index; if (TryGetIndexOfKey(key, &index)) { // The key was found; change its value. items[index]->Value = value; } else { // This key is not in the dictionary; add this // key/value pair. Add(key, value); } } }

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


IDictionary.Item プロパティ
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As IDictionary(Of TKey, TValue) Dim key As TKey Dim value As TValue value = instance(key) instance(key) = value
プロパティ値
指定したキーを持つ要素。


このプロパティを使用すると、myCollection[key] (Visual Basic では myCollection(key)) という構文を使用して、コレクション内の特定の要素にアクセスできます。
Item プロパティを使用すると、ディクショナリ内に存在しないキーの値を設定することで、新しい要素を追加することもできます (例 : C# の場合は myCollection["myNonexistentKey"] = myValue、Visual Basic の場合は myCollection("myNonexistentKey") = myValue) 。ただし、指定したキーがディクショナリに既に存在する場合、Item プロパティを設定すると、既存の値が上書きされます。対照的に、Add メソッドは既存の要素を変更しません。
オブジェクトが等しいかどうかを判断する方法で実装が異なります。たとえば、List クラスでは Comparer.Default が使用されます。また、Dictionary クラスでは、IComparer 実装をキーの比較に使うかどうかをユーザーが指定できます。
key を null 参照 (Visual Basic では Nothing) にできるかどうかは、実装によって異なります。

次のコード例では、Item プロパティ (C# ではインデクサ) を使用して値を取得し、要求されたキーが存在しないときに KeyNotFoundException がスローされる例を示し、またキーに関連付けられた値が置き換えられることも示します。
また、この例では、プログラムがディクショナリにないキー値を頻繁に試行する必要がある場合に、より効率的に値を取得する方法として TryGetValue メソッドを使用する方法も示します。
このコードは、コンパイルして実行することのできる例の一部です。詳細については、System.Collections.Generic.IDictionary のトピックを参照してください。
' When a program often has to try keys that turn out not to ' be in the dictionary, TryGetValue can be a more efficient ' way to retrieve values. Dim value As String = "" If openWith.TryGetValue("tif", value) Then Console.WriteLine("For key = ""tif"", value = {0}.", value) Else Console.WriteLine("Key = ""tif"" is not found.") End If
// When a program often has to try keys that turn out not to // be in the dictionary, TryGetValue can be a more efficient // way to retrieve values. string value = ""; if (openWith.TryGetValue("tif", out value)) { Console.WriteLine("For key = \"tif\", value = {0}.", value); } else { Console.WriteLine("Key = \"tif\" is not found."); }

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- IDictionary.Item プロパティのページへのリンク