IDictionary<TKey,TValue>.Item[TKey] プロパティ
定義
指定したキーを持つ要素を取得または設定します。Gets or sets the element with the specified key.
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
取得または設定する要素のキー。The key of the element to get or set.
プロパティ値
- TValue
指定したキーを持つ要素。The element with the specified key.
例外
key
が null
です。key
is null
.
プロパティは取得されますが、key
が見つかりません。The property is retrieved and key
is not found.
このプロパティが設定されていますが、IDictionary<TKey,TValue> が読み取り専用です。The property is set and the IDictionary<TKey,TValue> is read-only.
例
次のコード例では、 Item[] プロパティ (C# のインデクサー) を使用して値を取得し、 KeyNotFoundException 要求されたキーが存在しない場合にがスローされ、キーに関連付けられている値を置き換えることができることを示しています。The following code example uses the Item[] property (the indexer in C#) to retrieve values, demonstrating that a KeyNotFoundException is thrown when a requested key is not present, and showing that the value associated with a key can be replaced.
また、この例では、 TryGetValue プログラムがディクショナリに含まれていないキー値を試す必要がある場合に、より効率的に値を取得する方法として、メソッドを使用する方法も示しています。The example also shows how to use the TryGetValue method as a more efficient way to retrieve values if a program often must try key values that are not in the dictionary.
このコードは、コンパイルして実行できる大きな例の一部です。This code is part of a larger example that can be compiled and executed. 以下を参照してください。System.Collections.Generic.IDictionary<TKey,TValue>See System.Collections.Generic.IDictionary<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 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 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", 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.
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
注釈
このプロパティは、 myCollection[key]
(Visual Basic で) という構文を使用して、コレクション内の特定の要素にアクセスする機能を提供し myCollection(key)
ます。This property provides the ability to access a specific element in the collection by using the following syntax: myCollection[key]
(myCollection(key)
in Visual Basic).
また、プロパティを使用し Item[] て、ディクショナリに存在しないキーの値を設定して新しい要素を追加することもできます。たとえば、 myCollection["myNonexistentKey"] = myValue
C# の (Visual Basic) のようにし myCollection("myNonexistentKey") = myValue
ます。You can also use the Item[] property to add new elements by setting the value of a key that does not exist in the dictionary; for example, myCollection["myNonexistentKey"] = myValue
in C# (myCollection("myNonexistentKey") = myValue
in Visual Basic). ただし、指定したキーがディクショナリに既に存在する場合は、プロパティを設定すると Item[] 古い値が上書きされます。However, if the specified key already exists in the dictionary, setting the Item[] property overwrites the old value. これに対して、 Add メソッドは既存の要素を変更しません。In contrast, the Add method does not modify existing elements.
実装は、オブジェクトが等しいかどうかを判断する方法によって異なります。たとえば、クラスはを使用しますが、クラスでは、 List<T> Comparer<T>.Default Dictionary<TKey,TValue> IComparer<T> キーの比較に使用する実装をユーザーが指定できます。Implementations can vary in how they determine equality of objects; for example, the List<T> class uses Comparer<T>.Default, whereas the Dictionary<TKey,TValue> class allows the user to specify the IComparer<T> implementation to use for comparing keys.
C# 言語では、プロパティを実装する代わりに、 this キーワードを使用してインデクサーを定義し Item[] ます。The C# language uses the this keyword to define the indexers instead of implementing the Item[] property. Visual Basic は、Item[] を既定のプロパティとして実装しており、同様のインデックス機能を提供します。Visual Basic implements Item[] as a default property, which provides the same indexing functionality.
実装は、にすることが許可されているかどうかによって異なり key
null
ます。Implementations can vary in whether they allow key
to be null
.