Dictionary.System.Collections.IDictionary.Item プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Dictionary.System.Collections.IDictionary.Item プロパティの意味・解説 

Dictionary.System.Collections.IDictionary.Item プロパティ

メモ : このプロパティは、.NET Framework version 2.0 で新しく追加されたものです。

指定したキーの値を取得または設定します。

名前空間: System.Collections.Generic
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Private Property System.Collections.IDictionary.Item
 ( _
    key As Object _
) As Object Implements IDictionary.Item
Dim instance As Dictionary(Of
 TKey, TValue)
Dim key As Object
Dim value As Object

value = CType(instance, IDictionary).Item(key)

CType(instance, IDictionary).Item(key) = value
Object IDictionary.this [
    Object key
] { get; set; }
private:
virtual property Object^ System.Collections.IDictionary.Item [Object^] {
    Object^ get (Object^ key) sealed = IDictionary::default::get;
    void set (Object^ key, Object^ value) sealed
 = IDictionary::default::set;
}

パラメータ

key

取得する値のキー。

プロパティ値
key がディクショナリにない場合、または key が Dictionary のキー型 TKey に代入できる型ではない場合は、指定したキーに関連付けられた値または null 参照 (Visual Basic では Nothing)。

例外例外
例外の種類条件

ArgumentNullException

key が null 参照 (Visual Basic では Nothing) です。

ArgumentException

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

または

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

解説解説

このプロパティを使用すると、C# の myCollection[key] (Visual Basic では myCollection(key)) という構文を使用して、コレクション内の特定の値にアクセスできます。

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

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

使用例使用例

System.Collections.IDictionary インターフェイスの System.Collections.IDictionary.Item プロパティ (C# の場合はインデクサ) を Dictionary で使用する方法、およびこのプロパティと Dictionary.Item プロパティとの違いを次のコード例に示します。

このコード例では、Dictionary.Item プロパティ同様、Dictionary.System.Collections.IDictionary.Item プロパティを使用して既存のキーに関連付けられた値を変更したり、指定されたキーがディクショナリにない場合に新しいキー/値ペアを追加したりできることを示します。また、Dictionary.Item プロパティと異なり、Dictionary.System.Collections.IDictionary.Item プロパティは key がディクショナリにない場合に例外をスローしないで、代わりに null 参照を返すことを示します。最後に、Dictionary.System.Collections.IDictionary.Item プロパティを取得して、key が適切なデータ型ではない場合に null 参照を返す例や、プロパティを設定して、key が適切なデータ型でない場合に例外を返す例も示します。

次のコード例は、System.Collections.IDictionary.Add メソッドのトピックで取り上げている例および出力の一部分です。

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")
<br /><span space="preserve">...</span><br
 />        ' 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
<br /><span space="preserve">...</span><br
 />        ' 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"))
<br /><span space="preserve">...</span><br
 />
    End Sub

End Class
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");
<br /><span space="preserve">...</span><br /> 
       // 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.");
        }
<br /><span space="preserve">...</span><br /> 
       // 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"]);
<br /><span space="preserve">...</span><br /> 
   }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

Dictionary.System.Collections.IDictionary.Item プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Dictionary.System.Collections.IDictionary.Item プロパティのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS