SortedList<TKey,TValue>.IDictionary.Item[Object] プロパティ
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定したキーを持つ要素を取得または設定します。
property System::Object ^ System::Collections::IDictionary::Item[System::Object ^] { System::Object ^ get(System::Object ^ key); void set(System::Object ^ key, System::Object ^ value); };
object System.Collections.IDictionary.Item[object key] { get; set; }
member this.System.Collections.IDictionary.Item(obj) : obj with get, set
Property Item(key As Object) As Object Implements IDictionary.Item
パラメーター
- key
- Object
取得または設定する要素のキー。
プロパティ値
指定したキーを持つ要素。または、keyがディクショナリに含まれていないか、keyがSortedList<TKey,TValue>のキー型TKeyに割り当てられない型の場合にnullします。
実装
例外
key は nullです。
値が割り当てられ、keyはSortedList<TKey,TValue>のキー型TKeyに割り当てられない型です。
-または-
値が割り当てられ、SortedList<TKey,TValue>の値型TValueに割り当てることができない型です。
例
次のコード例は、System.Collections.IDictionary インターフェイスの IDictionary.Item[] プロパティ (C#のインデクサー) をSortedList<TKey,TValue>と共に使用する方法と、プロパティが SortedList<TKey,TValue>.Item[] プロパティと異なる方法を示しています。
この例では、 SortedList<TKey,TValue>.Item[] プロパティと同様に、 SortedList<TKey,TValue>.IDictionary.Item[] プロパティは既存のキーに関連付けられている値を変更でき、指定したキーが並べ替えられたリストにない場合に新しいキーと値のペアを追加するために使用できることを示しています。 この例では、SortedList<TKey,TValue>.Item[] プロパティとは異なり、keyが並べ替えられたリストにない場合、SortedList<TKey,TValue>.IDictionary.Item[] プロパティは例外をスローせず、代わりに null 参照を返すことも示しています。 最後に、 SortedList<TKey,TValue>.IDictionary.Item[] プロパティを取得すると、 key が正しいデータ型でない場合は null 参照が返され、 key が正しいデータ型でない場合はプロパティを設定すると例外がスローされることを示します。
このコード例は、 IDictionary.Add メソッドに対して提供される出力を含む、より大きな例の一部です。
using System;
using System.Collections;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// Create a new sorted list of strings, with string keys,
// and access it using the IDictionary interface.
//
IDictionary openWith = new SortedList<string, string>();
// Add some elements to the sorted list. 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");
Imports System.Collections
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
' Create a new sorted list of strings, with string keys,
' and access it using the IDictionary interface.
'
Dim openWith As IDictionary = _
New sortedList(Of String, String)
' Add some elements to the sorted list. 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")
// 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.");
}
' 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
// Unlike the default Item property on the SorteList class
// itself, IDictionary.Item does not throw an exception
// if the requested key is not in the sorted list.
Console.WriteLine("For key = \"tif\", value = {0}.",
openWith["tif"]);
' Unlike the default Item property on the SortedList class
' itself, IDictionary.Item does not throw an exception
' if the requested key is not in the sorted list.
Console.WriteLine("For key = ""tif"", value = {0}.", _
openWith("tif"))
}
}
End Sub
End Class
注釈
keyがSortedList<TKey,TValue>のキー型TKeyに割り当てられない型の場合、このプロパティはnullを返します。
このプロパティは、次の構文を使用してコレクション内の特定の要素にアクセスする機能を提供します: myCollection[key]。
Item[] プロパティを使用して、ディクショナリに存在しないキーの値 (myCollection["myNonexistentKey"] = myValueなど) を設定して、新しい要素を追加することもできます。 ただし、指定したキーがディクショナリに既に存在する場合は、 Item[] プロパティを設定すると、古い値が上書きされます。 これに対し、 Add メソッドは既存の要素を変更しません。
C# 言語では、IDictionary.Item[] プロパティを実装する代わりに、このキーワードを使用してインデクサーを定義します。 Visual Basic では、 IDictionary.Item[] を既定のプロパティとして実装します。このプロパティは、同じインデックス作成機能を提供します。
このプロパティの値の取得は O (ログ n) 操作であり、n は Count。 キーが既にSortedList<TKey,TValue>内にある場合、プロパティの設定は O (ログ n) 操作です。 キーがリストにない場合、プロパティの設定は、並べ替えられていないデータに対する O (n) 操作、またはリストの末尾に新しい要素が追加された場合は O (ログ n) です。 挿入によってサイズが変更された場合、操作は O(n) になります。