📌  相关文章
📜  C#|检查哈希表是否包含特定值

📅  最后修改于: 2021-05-29 23:57:51             🧑  作者: Mango

Hashtable类表示键和值对的集合,这些键和值对是根据键的哈希码进行组织的。该键用于访问集合中的项目。 Hashtable.ContainsValue(Object)方法用于检查Hashtable是否包含特定值。

句法:

public virtual bool ContainsValue(object value);

范围:

返回类型:如果Hashtable包含具有指定的元素,则此方法返回true ,否则返回false 。此方法的返回类型为System.Boolean

下面给出了一些示例,以更好地理解实现:

范例1:

// C# code to check if the HashTable
// contains a specific Value
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 Value or not.
        if (myTable.ContainsValue("c++"))
            Console.WriteLine("myTable contains the Value");
        else
            Console.WriteLine("myTable doesn't contain the Value");
    }
}
输出:
myTable contains the Value

范例2:

// C# code to check if the HashTable
// contains a specific Value
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 Value or not.
        if (myTable.ContainsKey("Ocean"))
            Console.WriteLine("myTable contains the Value");
        else
            Console.WriteLine("myTable doesn't contain the Value");
    }
}
输出:
myTable doesn't contain the Value

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.hashtable.containsvalue?view=netframework-4.7.2