Dictionary<TKey,TValue>.TryGetValue(TKey, TValue) メソッド

定義

指定したキーに関連付けられている値を取得します。

public:
 virtual bool TryGetValue(TKey key, [Runtime::InteropServices::Out] TValue % value);
public bool TryGetValue (TKey key, out TValue value);
abstract member TryGetValue : 'Key * 'Value -> bool
override this.TryGetValue : 'Key * 'Value -> bool
Public Function TryGetValue (key As TKey, ByRef value As TValue) As Boolean

パラメーター

key
TKey

取得する値のキー。

value
TValue

このメソッドから制御が戻るときに、キーが見つかった場合は、指定したキーに関連付けられている値が格納されます。それ以外の場合は value パラメーターの型に対する既定の値。 このパラメーターは初期化せずに渡されます。

戻り値

指定したキーを持つ要素が true に格納されている場合は Dictionary<TKey,TValue>。それ以外の場合は false

実装

例外

keynullです。

この例では、 メソッドを使用 TryGetValue して、ディクショナリにないキーを頻繁に試行するプログラムの値を取得するより効率的な方法を示します。 これに対して、この例では、存在しないキーを Item[] 取得しようとしたときに、 プロパティ (C# のインデクサー) が例外をスローする方法も示しています。

このコード例は、 クラスに対して提供される大きな例の Dictionary<TKey,TValue> 一部です (openWith は、この例で使用する Dictionary の名前です)。

// 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
// 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

注釈

このメソッドは、 メソッドと プロパティの ContainsKey 機能を Item[] 組み合わせたものです。

キーが見つからない場合、 value パラメーターは型の適切な既定値 (整数型 TValueの場合は 0 (ゼロ)、ブール型の場合は 0、 falsenull 参照型の場合は を取得します。

コードが TryGetValue ディクショナリにないキーへのアクセスを頻繁に試みる場合は、 メソッドを使用します。 このメソッドを使用すると、 プロパティによってItem[]スローされた をKeyNotFoundExceptionキャッチするよりも効率的です。

このメソッドは O(1) 操作に近づく。

適用対象

こちらもご覧ください