Hashtable类表示 键和值对 根据密钥的哈希代码进行组织。密钥用于访问集合中的项目。 哈希表。ContainsKey(Object)方法 用于检查哈希表是否包含特定键。
null
语法:
public virtual bool ContainsKey(object key);
参数:
关键: 类型的关键 系统对象 在哈希表中定位。
返回类型: 它回来了 符合事实的 如果哈希表包含具有指定键的元素,则为, 错误的 。此方法的返回类型为 系统布尔值 .
例外情况: 这种方法可以提供 无理例外 如果 钥匙 是空的。
下面给出了一些例子,以更好地理解实施:
例1:
// C# code to check if the HashTable // contains a specific key using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a Hashtable Hashtable myTable = new Hashtable(); // Adding elements in Hashtable myTable.Add( "g" , "geeks" ); myTable.Add( "c" , "c++" ); myTable.Add( "d" , "data structures" ); myTable.Add( "q" , "quiz" ); // check if the HashTable contains // the required key or not. if (myTable.ContainsKey( "c" )) Console.WriteLine( "myTable contains the key" ); else Console.WriteLine( "myTable doesn't contain the key" ); } } |
输出:
myTable contains the key
例2:
// C# code to check if the HashTable // contains a specific key using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a Hashtable Hashtable myTable = new Hashtable(); // Adding elements in Hashtable myTable.Add( "India" , "Country" ); myTable.Add( "Chandigarh" , "City" ); myTable.Add( "Mars" , "Planet" ); myTable.Add( "China" , "Country" ); // check if the HashTable contains // the required key or not. if (myTable.ContainsKey( "Earth" )) Console.WriteLine( "myTable contains the key" ); else Console.WriteLine( "myTable doesn't contain the key" ); } } |
输出:
myTable doesn't contain the key
参考:
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END