Use the ContainsValue() method to check if Hashtable contains a value : Hashtable « Data Structure « C# / CSharp Tutorial






using System;
using System.Collections;

class MainClass
{

  public static void Main()
  {
    Hashtable myHashtable = new Hashtable();

    myHashtable.Add("AL", "Alabama");
    myHashtable.Add("CA", "California");
    myHashtable.Add("FL", "Florida");
    myHashtable.Add("NY", "New York");
    myHashtable.Add("WY", "Wyoming");

    foreach (string myKey in myHashtable.Keys)
    {
      Console.WriteLine("myKey = " + myKey);
    }
    foreach(string myValue in myHashtable.Values)
    {
      Console.WriteLine("myValue = " + myValue);
    }
    
    if (myHashtable.ContainsValue("Florida"))
    {
      Console.WriteLine("myHashtable contains the value Florida");
    }
  }
}
myKey = NY
myKey = CA
myKey = FL
myKey = WY
myKey = AL
myValue = New York
myValue = California
myValue = Florida
myValue = Wyoming
myValue = Alabama
myHashtable contains the value Florida








11.29.Hashtable
11.29.1.Add elements to the table and Use the keys to obtain the values
11.29.2.Add key-value pair to Hashtable by using the indexer
11.29.3.Use foreach statement to loop through all keys in a hashtable
11.29.4.Clear all key/value pairs in a Hashtable
11.29.5.Mark a Hashtable to be Synchronized
11.29.6.Remove key/value pairs from Hashtable
11.29.7.Use the ContainsKey() method to check if Hashtable contains a key
11.29.8.Use the ContainsValue() method to check if Hashtable contains a value
11.29.9.Use the Remove() method to remove FL from Hashtable
11.29.10.Copy the keys from Hashtable into an array using the CopyTo() method and then display the array contents
11.29.11.Copy the values from Hashtable into an array using the CopyTo() method and then display the array contents