SortedDictionary.System.Collections.IDictionary.Item プロパティ
アセンブリ: 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
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 が SortedDictionary のキー型 TKey に代入できる型ではない場合は、指定したキーに関連付けられた要素または null 参照 (Visual Basic では Nothing)。


このプロパティを使用すると、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 /> } }

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


Weblioに収録されているすべての辞書からSortedDictionary.System.Collections.IDictionary.Item プロパティを検索する場合は、下記のリンクをクリックしてください。

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