C# Hashtable ContainsValue

Description

Hashtable ContainsValue determines whether the Hashtable contains a specific value.

Syntax

Hashtable.ContainsValue has the following syntax.


public virtual bool ContainsValue(
  Object value
)

Parameters

Hashtable.ContainsValue has the following parameters.

  • value - The value to locate in the Hashtable. The value can be null.

Returns

Hashtable.ContainsValue method returns true if the Hashtable contains an element with the specified value; otherwise, false.

Example

We can use the ContainsValue() method to check if Hashtable contains a value.


using System;/*from  w  w  w .j  a  v a2s  .  c o m*/
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");
    }
  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack