Assembly : System (dans system.dll)
SyntaxePublic Function ContainsKey ( _ key As TKey _ ) As Boolean
Dim instance As SortedDictionary(Of TKey, TValue) Dim key As TKey Dim returnValue As Boolean returnValue = instance.ContainsKey(key)
public bool ContainsKey ( TKey key )
public: virtual bool ContainsKey ( TKey key ) sealed
public final boolean ContainsKey (
TKey key
)
public final function ContainsKey ( key : TKey ) : boolean
Paramètres
- key
Clé à rechercher dans SortedDictionary.
Valeur de retour
true si SortedDictionary contient un élément correspondant à la clé spécifiée ; sinon, false.
ExempleL'exemple de code suivant montre comment utiliser la méthode ContainsKey pour tester si une clé existe avant d'appeler la méthode Add. Il indique également comment utiliser la méthode TryGetValue pour récupérer des valeurs, ce qui représente une façon efficace de récupérer des valeurs lorsqu'un programme essaie fréquemment des clés qui ne sont pas dans le dictionnaire. Pour finir, il indique le moyen le moins efficace de tester si les clés existent, à l'aide de la propriété Item (l'indexeur en C#).
Cet exemple de code fait partie d'un exemple plus développé fourni pour la classe SortedDictionary.
' ContainsKey can be used to test keys before inserting ' them. If Not openWith.ContainsKey("ht") Then openWith.Add("ht", "hypertrm.exe") Console.WriteLine("Value added for key = ""ht"": {0}", _ openWith("ht")) End If ... ' 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 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 can be used to test keys before inserting // them. if (!openWith.ContainsKey("ht")) { openWith.Add("ht", "hypertrm.exe"); Console.WriteLine("Value added for key = \"ht\": {0}", openWith["ht"]); } ... // 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."); }
Plates-formesWindows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
Outils (masquer)
S'enregistrer
Liste des Membres
Qui est en ligne?
FAQ