|
Cet article a fait l'objet d'une traduction automatique. Déplacez votre pointeur sur les phrases de l'article pour voir la version originale de ce texte. Informations supplémentaires.
|
Traduction
Source
|
Dictionary<TKey, TValue>.TryGetValue, méthode
Espace de noms : System.Collections.Generic
Assembly : mscorlib (dans mscorlib.dll)
Paramètres
- key
- Type : TKey
Clé de la valeur à obtenir.
- value
- Type : TValue
Cette méthode retourne la valeur associée à la clé spécifiée, si la clé est trouvée ; sinon, retourne la valeur par défaut pour le type du paramètre value. Ce paramètre est passé sans être initialisé.
Valeur de retour
Type : System.BooleanImplémentations
IDictionary<TKey, TValue>.TryGetValue(TKey, TValue)IReadOnlyDictionary<TKey, TValue>.TryGetValue(TKey, TValue)
| Exception | Condition |
|---|---|
| ArgumentNullException |
// 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."); } ... // 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."); }
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (rôle principal du serveur non pris en charge), Windows Server 2008 R2 (rôle principal du serveur pris en charge avec SP1 ou version ultérieure ; Itanium non pris en charge)
Le .NET Framework ne prend pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.