SortedDictionary<TKey,TValue>.Item[TKey] プロパティ
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定したキーに関連付けられている値を取得または設定します。
public:
property TValue default[TKey] { TValue get(TKey key); void set(TKey key, TValue value); };
public TValue this[TKey key] { get; set; }
member this.Item('Key) : 'Value with get, set
Default Public Property Item(key As TKey) As TValue
パラメーター
- key
- TKey
取得または設定する値のキー。
プロパティ値
指定したキーに関連付けられている値。 指定したキーが見つからない場合、get 操作は KeyNotFoundExceptionをスローし、set 操作は指定したキーを持つ新しい要素を作成します。
実装
例外
key は nullです。
プロパティが取得され、 key がコレクションに存在しません。
例
次のコード例では、 Item[] プロパティ (C# のインデクサー) を使用して値を取得し、要求されたキーが存在しない場合に KeyNotFoundException がスローされることを示し、キーに関連付けられている値を置き換えることができることを示します。
この例では、 TryGetValue メソッドを使用して、プログラムがディクショナリにないキー値を頻繁に試す必要がある場合に、より効率的に値を取得する方法も示します。
このコード例は、 SortedDictionary<TKey,TValue> クラスに提供されるより大きな例の一部です。
// 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 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 indexer throws an exception if the requested key is
// not in the dictionary.
try
{
Console.WriteLine("For key = \"tif\", value = {0}.",
openWith["tif"]);
}
catch (KeyNotFoundException)
{
Console.WriteLine("Key = \"tif\" is not found.");
}
' The default Item property throws an exception if the requested
' key is not in the dictionary.
Try
Console.WriteLine("For key = ""tif"", value = {0}.", _
openWith("tif"))
Catch
Console.WriteLine("Key = ""tif"" is not found.")
End Try
// 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.");
}
' 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
注釈
このプロパティは、次の C# 構文を使用してコレクション内の特定の要素にアクセスする機能を提供します: myCollection[key] (Visual Basic の myCollection(key))。
Item[] プロパティを使用して、SortedDictionary<TKey,TValue>に存在しないキーの値 (myCollection["myNonexistentKey"] = myValueなど) を設定して、新しい要素を追加することもできます。 ただし、指定したキーが既に SortedDictionary<TKey,TValue>に存在する場合は、 Item[] プロパティを設定すると、古い値が上書きされます。 これに対し、 Add メソッドは既存の要素を変更しません。
キーを nullすることはできませんが、 TValue 値型が参照型の場合は、値を指定できます。
C# 言語では、Item[] プロパティを実装する代わりに、このキーワードを使用してインデクサーを定義します。 Visual Basic では、 Item[] を既定のプロパティとして実装します。このプロパティは、同じインデックス作成機能を提供します。
このプロパティの値の取得は O (ログ n) 操作です。プロパティの設定は O (ログ n) 操作でもあります。