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

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

IDictionary.Item プロパティ

指定したキー持つ要素取得または設定します

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

例外例外
例外種類条件

ArgumentNullException

keynull 参照 (Visual Basic では Nothing) です。

NotSupportedException

このプロパティ設定されていますが、IDictionary オブジェクト読み取り専用です。

または

このプロパティ設定されていますが、keyコレクション内に存在しません。また、IDictionary固定サイズです。

解説解説

このプロパティでは、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);
            }
        }
    }
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

IDictionary.Item プロパティ

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

指定したキー持つ要素取得または設定します

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

Default Property Item ( _
    key As TKey _
) As TValue
Dim instance As IDictionary(Of
 TKey, TValue)
Dim key As TKey
Dim value As TValue

value = instance(key)

instance(key) = value
TValue this [
    TKey key
] { get; set; }
property TValue default [TKey] {
    TValue get (TKey key);
    void set (TKey key, TValue value);
}
/** @property */
TValue get_Item (TKey key)

/** @property */
void set_Item (TKey key, TValue value)

パラメータ

key

取得または設定する要素キー

プロパティ
指定したキー持つ要素

例外例外
例外種類条件

ArgumentNullException

keynull 参照 (Visual Basic では Nothing) です。

KeyNotFoundException

プロパティ取得されますが、key が見つかりません。

NotSupportedException

このプロパティ設定されていますが、IDictionary が読み取り専用です。

解説解説

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

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

オブジェクト等しかどうか判断する方法実装異なります。たとえば、List クラスでは Comparer.Default が使用されます。また、Dictionary クラスでは、IComparer 実装キー比較に使うかどうかユーザー指定できます

keynull 参照 (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.");
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
IDictionary ジェネリック インターフェイス
IDictionary メンバ
System.Collections.Generic 名前空間
IDictionary.Add メソッド
IDictionary.ContainsKey メソッド
ICollection.IsReadOnly プロパティ



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「IDictionary.Item プロパティ」の関連用語

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

   

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



IDictionary.Item プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS