IDictionary<TKey,TValue>.Item[TKey] IDictionary<TKey,TValue>.Item[TKey] IDictionary<TKey,TValue>.Item[TKey] IDictionary<TKey,TValue>.Item[TKey] Property

定義

指定したキーを持つ要素を取得または設定します。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 TKey TKey TKey

取得または設定する要素のキー。The key of the element to get or set.

プロパティ値

TValue TValue TValue TValue

指定したキーを持つ要素。The element with the specified key.

例外

プロパティは取得されますが、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[TKey] 、プロパティ (のC#インデクサー) を使用して値を取得KeyNotFoundExceptionします。これは、要求されたキーが存在しない場合にがスローされ、キーに関連付けられている値を置き換えることができることを示しています。The following code example uses the Item[TKey] 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] (myCollection(key) Visual Basic で) という構文を使用して、コレクション内の特定の要素にアクセスする機能を提供します。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[TKey]しないキーの値を設定することによって、プロパティを使用して新しい要素を追加することC#もmyCollection("myNonexistentKey") = myValueできます。たとえば、 myCollection["myNonexistentKey"] = myValue (Visual Basic の) のようになります。You can also use the Item[TKey] 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[TKey]は、プロパティを設定すると古い値が上書きされます。However, if the specified key already exists in the dictionary, setting the Item[TKey] property overwrites the old value. これに対してAdd 、メソッドは既存の要素を変更しません。In contrast, the Add method does not modify existing elements.

実装は、オブジェクトが等しいかどうかを判断する方法によって異なります。たとえばList<T> 、クラスはをDictionary<TKey,TValue>使用Comparer<T>.Defaultしますが、クラスでは、キーの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#言語ではItem[TKey]プロパティを実装する代わりに、this キーワードを使用してインデクサーを定義します。The C# language uses the this keyword to define the indexers instead of implementing the Item[TKey] property. Visual Basic は、Item[TKey] を既定のプロパティとして実装しており、同様のインデックス機能を提供します。Visual Basic implements Item[TKey] as a default property, which provides the same indexing functionality.

実装は、にすることがkey許可さnullれているかどうかによって異なります。Implementations can vary in whether they allow key to be null.

適用対象

こちらもご覧ください