IDictionary.TryGetValue メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > IDictionary.TryGetValue メソッドの意味・解説 

IDictionary.TryGetValue メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

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

名前空間: System.Collections.Generic
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Function TryGetValue ( _
    key As TKey, _
    <OutAttribute> ByRef value As TValue
 _
) As Boolean
Dim instance As IDictionary(Of
 TKey, TValue)
Dim key As TKey
Dim value As TValue
Dim returnValue As Boolean

returnValue = instance.TryGetValue(key, value)
bool TryGetValue (
    TKey key,
    out TValue value
)
bool TryGetValue (
    TKey key, 
    [OutAttribute] TValue% value
)
boolean TryGetValue (
    TKey key, 
    /** @attribute OutAttribute() */ /** @ref */ TValue value
)
JScript では、値型引数参照渡しされません。

パラメータ

key

値を取得する対象キー

value

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

戻り値
指定したキー持つ要素が IDictionary を実装するオブジェクト格納されている場合trueそれ以外場合false

例外例外
例外種類条件

ArgumentNullException

keynull 参照 (Visual Basic では Nothing) です。

解説解説
使用例使用例

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

このコードは、コンパイルして実行することのできる例の一部です。詳細については、System.Collections.Generic.IDictionaryトピック参照してください

        ' 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
<br /><span space="preserve">...</span><br
 />        ' 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.");
        }
<br /><span space="preserve">...</span><br /> 
       // 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.");
        }
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
IDictionary ジェネリック インターフェイス
IDictionary メンバ
System.Collections.Generic 名前空間
IDictionary.ContainsKey メソッド
Item



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

IDictionary.TryGetValue メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



IDictionary.TryGetValue メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS