IDictionary<TKey,TValue>.Item[TKey] プロパティ

定義

指定したキーを持つ要素を取得または設定します。

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

取得または設定する要素のキー。

プロパティ値

TValue

指定したキーを持つ要素。

例外

keynullです。

プロパティが取得され、 key が見つかりません。

プロパティが設定され、 IDictionary<TKey,TValue> が読み取り専用です。

次のコード例では、 Item[] プロパティ (C# のインデクサー) を使用して値を取得し、要求されたキーが存在しない場合に KeyNotFoundException がスローされることを示し、キーに関連付けられている値を置き換えることができることを示します。

この例では、 TryGetValue メソッドを使用して、プログラムがディクショナリにないキー値を頻繁に試す必要がある場合に、より効率的に値を取得する方法も示します。

このコードは、コンパイルして実行できるより大きな例の一部です。 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 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

注釈

このプロパティは、次の構文を使用してコレクション内の特定の要素にアクセスする機能を提供します: myCollection[key] (Visual BasicのmyCollection(key))。

Item[] プロパティを使用して、ディクショナリに存在しないキーの値を設定することで、新しい要素を追加することもできます。たとえば、C# の myCollection["myNonexistentKey"] = myValue (Visual Basic では myCollection("myNonexistentKey") = myValue)。 ただし、指定したキーがディクショナリに既に存在する場合は、 Item[] プロパティを設定すると、古い値が上書きされます。 これに対し、 Add メソッドは既存の要素を変更しません。

実装は、オブジェクトの等価性を決定する方法によって異なる場合があります。たとえば、 List<T> クラスは Comparer<T>.Defaultを使用しますが、 Dictionary<TKey,TValue> クラスでは、キーの比較に使用する IComparer<T> 実装をユーザーが指定できます。

C# 言語では、Item[] プロパティを実装する代わりに、このキーワードを使用してインデクサーを定義します。 Visual Basic では、 Item[] を既定のプロパティとして実装します。このプロパティは、同じインデックス作成機能を提供します。

実装は、 keynullできるかどうかによって異なる場合があります。

適用対象

こちらもご覧ください