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

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

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

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

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

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

Private Property System.Collections.IDictionary.Item
 ( _
    key As Object _
) As Object Implements IDictionary.Item
Dim instance As SortedDictionary(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 が SortedDictionary のキーTKey代入できる型ではない場合は、指定したキー関連付けられた要素または null 参照 (Visual Basic では Nothing)。

例外例外
例外種類条件

ArgumentNullException

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

ArgumentException

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

または

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

解説解説

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

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

このプロパティ値を取得することは、O(log n) 操作なりますまた、このプロパティ設定することも O(log n) 操作なります

使用例使用例

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

このコード例では、SortedDictionary.Item プロパティ同様、SortedDictionary.System.Collections.IDictionary.Item プロパティ使用して既存キー関連付けられた値を変更したり、指定されキーがディクショナリにない場合新しキー/値ペア追加したできること示しますまた、SortedDictionary.Item プロパティ異なりSortedDictionary.System.Collections.IDictionary.Item プロパティkey がディクショナリにない場合例外スローしないで、代わりに null 参照返すことを示します最後にSortedDictionary.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 sorted dictionary of strings, with string keys
,
        ' and access it using the IDictionary interface.
        '
        Dim openWith As IDictionary = _
            New SortedDictionary(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 sorted dictionary of strings, with string keys
,
        // and access it using the IDictionary interface.
        //
        IDictionary openWith = new SortedDictionary<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 /> 
   }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SortedDictionary ジェネリック クラス
SortedDictionary メンバ
System.Collections.Generic 名前空間
Add


このページでは「.NET Framework クラス ライブラリ リファレンス」からSortedDictionary.System.Collections.IDictionary.Item プロパティを検索した結果を表示しています。
Weblioに収録されているすべての辞書からSortedDictionary.System.Collections.IDictionary.Item プロパティを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からSortedDictionary.System.Collections.IDictionary.Item プロパティ を検索

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

辞書ショートカット

すべての辞書の索引

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

   

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



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

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

©2025 GRAS Group, Inc.RSS