Export (0) Print
Expand All

Hashtable.ContainsKey Method

Determines whether the Hashtable contains a specific key.

[Visual Basic]
Public Overridable Function ContainsKey( _
   ByVal key As Object _
) As Boolean
[C#]
public virtual bool ContainsKey(
 object key
);
[C++]
public: virtual bool ContainsKey(
 Object* key
);
[JScript]
public function ContainsKey(
   key : Object
) : Boolean;

Parameters

key
The key to locate in the Hashtable.

Return Value

true if the Hashtable contains an element with the specified key; otherwise, false.

Exceptions

Exception Type Condition
ArgumentNullException key is a null reference (Nothing in Visual Basic).

Remarks

This implementation is close to O(1) in most cases.

This method behaves exactly as Contains.

Example

The following example shows how to determine whether the Hashtable contains a specific element.

[Visual Basic] 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesHashtable    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Hashtable.
        Dim myHT As New Hashtable()
        myHT.Add(0, "zero")
        myHT.Add(1, "one")
        myHT.Add(2, "two")
        myHT.Add(3, "three")
        myHT.Add(4, "four")
        
        ' Displays the values of the Hashtable.
        Console.WriteLine("The Hashtable contains the following values:")
        PrintIndexAndKeysAndValues(myHT)
        
        ' Searches for a specific key.
        Dim msg As String
        Dim myKey As Integer = 2
        If myHT.ContainsKey(myKey) Then
            msg = "in the Hashtable"
        Else
            msg = "NOT in the Hashtable"
        End If
        Console.WriteLine("The key ""{0}"" is {1}.", myKey, msg)
        myKey = 6
        If myHT.ContainsKey(myKey) Then
            msg = "in the Hashtable"
        Else
            msg = "NOT in the Hashtable"
        End If
        Console.WriteLine("The key ""{0}"" is {1}.", myKey, msg)
        
        ' Searches for a specific value.
        Dim myValue As String = "three"
        If myHT.ContainsValue(myValue) Then
            msg = "in the Hashtable"
        Else
            msg = "NOT in the Hashtable"
        End If
        Console.WriteLine("The value ""{0}"" is {1}.", myValue, msg)
        myValue = "nine"
        If myHT.ContainsValue(myValue) Then
            msg = "in the Hashtable"
        Else
            msg = "NOT in the Hashtable"
        End If
        Console.WriteLine("The value ""{0}"" is {1}.", myValue, msg)
    End Sub
    
        
    Public Shared Sub PrintIndexAndKeysAndValues(myList As Hashtable)
        Dim myEnumerator As IDictionaryEnumerator = myList.GetEnumerator()
        Dim i As Integer = 0
        Console.WriteLine(ControlChars.Tab + "-INDEX-" + ControlChars.Tab _
           + "-KEY-" + ControlChars.Tab + "-VALUE-")
        While myEnumerator.MoveNext()
            i = i + 1
            Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
               + "{1}" + ControlChars.Tab + "{2}", i, myEnumerator.Key, _
               myEnumerator.Value)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' The Hashtable contains the following values:
'     -INDEX-    -KEY-    -VALUE-
'     [0]:    4    four
'     [1]:    3    three
'     [2]:    2    two
'     [3]:    1    one
'     [4]:    0    zero
' 
' The key "2" is in the Hashtable.
' The key "6" is NOT in the Hashtable.
' The value "three" is in the Hashtable.
' The value "nine" is NOT in the Hashtable. 

[C#] 
using System;
using System.Collections;
public class SamplesHashtable  {

   public static void Main()  {

      // Creates and initializes a new Hashtable.
      Hashtable myHT = new Hashtable();
      myHT.Add( 0, "zero" );
      myHT.Add( 1, "one" );
      myHT.Add( 2, "two" );
      myHT.Add( 3, "three" );
      myHT.Add( 4, "four" );

      // Displays the values of the Hashtable.
      Console.WriteLine( "The Hashtable contains the following values:" );
      PrintIndexAndKeysAndValues( myHT );

      // Searches for a specific key.
      int myKey = 2;
      Console.WriteLine( "The key \"{0}\" is {1}.", myKey, myHT.ContainsKey( myKey ) ? "in the Hashtable" : "NOT in the Hashtable" );
      myKey = 6;
      Console.WriteLine( "The key \"{0}\" is {1}.", myKey, myHT.ContainsKey( myKey ) ? "in the Hashtable" : "NOT in the Hashtable" );

      // Searches for a specific value.
      String myValue = "three";
      Console.WriteLine( "The value \"{0}\" is {1}.", myValue, myHT.ContainsValue( myValue ) ? "in the Hashtable" : "NOT in the Hashtable" );
      myValue = "nine";
      Console.WriteLine( "The value \"{0}\" is {1}.", myValue, myHT.ContainsValue( myValue ) ? "in the Hashtable" : "NOT in the Hashtable" );
   }


   public static void PrintIndexAndKeysAndValues( Hashtable myList )  {
      IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
      int i = 0;
      Console.WriteLine( "\t-INDEX-\t-KEY-\t-VALUE-" );
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "\t[{0}]:\t{1}\t{2}", i++, myEnumerator.Key, myEnumerator.Value );
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

The Hashtable contains the following values:
    -INDEX-    -KEY-    -VALUE-
    [0]:    4    four
    [1]:    3    three
    [2]:    2    two
    [3]:    1    one
    [4]:    0    zero

The key "2" is in the Hashtable.
The key "6" is NOT in the Hashtable.
The value "three" is in the Hashtable.
The value "nine" is NOT in the Hashtable.
*/ 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;

void PrintIndexAndKeysAndValues( Hashtable* myList )  {
   IDictionaryEnumerator* myEnumerator = myList->GetEnumerator();
   int i = 0;
   Console::WriteLine( S"\t-INDEX-\t-KEY-\t-VALUE-" );
   while ( myEnumerator->MoveNext() )
      Console::WriteLine( S"\t[{0}]:\t{1}\t{2}", __box(i++), myEnumerator->Key, myEnumerator->Value );
   Console::WriteLine();
}

int main()  {
   // Creates and initializes a new Hashtable.
   Hashtable* myHT = new Hashtable();
   myHT->Add( __box(0), S"zero" );
   myHT->Add( __box(1), S"one" );
   myHT->Add( __box(2), S"two" );
   myHT->Add( __box(3), S"three" );
   myHT->Add( __box(4), S"four" );

   // Displays the values of the Hashtable.
   Console::WriteLine( S"The Hashtable contains the following values:" );
   PrintIndexAndKeysAndValues( myHT );

   // Searches for a specific key.
   int myKey = 2;
   Console::WriteLine( S"The key \"{0}\" is {1}.", __box(myKey),
      myHT->ContainsKey( __box(myKey) ) ? S"in the Hashtable" : S"NOT in the Hashtable" );
   myKey = 6;
   Console::WriteLine( S"The key \"{0}\" is {1}.", __box(myKey),
      myHT->ContainsKey( __box(myKey) ) ? S"in the Hashtable" : S"NOT in the Hashtable" );

   // Searches for a specific value.
   String* myValue = S"three";
   Console::WriteLine( S"The value \"{0}\" is {1}.", myValue,
      myHT->ContainsValue( myValue ) ? S"in the Hashtable" : S"NOT in the Hashtable" );
   myValue = S"nine";
   Console::WriteLine( S"The value \"{0}\" is {1}.", myValue, 
      myHT->ContainsValue( myValue ) ? S"in the Hashtable" : S"NOT in the Hashtable" );
}

 /*
 This code produces the following output.

 The Hashtable contains the following values:
        -INDEX- -KEY-   -VALUE-
        [0]:    4       four
        [1]:    3       three
        [2]:    2       two
        [3]:    1       one
        [4]:    0       zero

The key "2" is in the Hashtable.
The key "6" is NOT in the Hashtable.
The value "three" is in the Hashtable.
The value "nine" is NOT in the Hashtable.
 */

[JScript] 
import System
import System.Collections
import Microsoft.VisualBasic

// Creates and initializes a new Hashtable.
var myHT : Hashtable = new Hashtable()
myHT.Add(0, "zero")
myHT.Add(1, "one")
myHT.Add(2, "two")
myHT.Add(3, "three")
myHT.Add(4, "four")

// Displays the values of the Hashtable.
Console.WriteLine("The Hashtable contains the following values:")
PrintIndexAndKeysAndValues(myHT)

// Searches for a specific key.
var msg : String
var myKey : int = 2
if(myHT.ContainsKey(myKey))
    msg = "in the Hashtable"
else
    msg = "NOT in the Hashtable"
Console.WriteLine("The key \"{0}\" is {1}.", myKey, msg)
myKey = 6
if(myHT.ContainsKey(myKey))
    msg = "in the Hashtable"
else
    msg = "NOT in the Hashtable"
Console.WriteLine("The key \"{0}\" is {1}.", myKey, msg)

// Searches for a specific value.
var myValue : String = "three"
if(myHT.ContainsValue(myValue))
    msg = "in the Hashtable"
else
    msg = "NOT in the Hashtable"
Console.WriteLine("The value \"{0}\" is {1}.", myValue, msg)
myValue = "nine"
if(myHT.ContainsValue(myValue))
    msg = "in the Hashtable"
else
    msg = "NOT in the Hashtable"
Console.WriteLine("The value \"{0}\" is {1}.", myValue, msg)

function PrintIndexAndKeysAndValues(myList : Hashtable){
    var myEnumerator : IDictionaryEnumerator = myList.GetEnumerator()
    var i : int = 0
    Console.WriteLine("\t-INDEX-\t-KEY-\t-VALUE-")
    while(myEnumerator.MoveNext()){
        Console.WriteLine("\t[{0}]:\t{1}\t{2}", i++, myEnumerator.Key,
            myEnumerator.Value)
    }
    Console.WriteLine()
}

// This code produces the following output.
// 
// The Hashtable contains the following values:
//     -INDEX-    -KEY-    -VALUE-
//     [0]:       4        four
//     [1]:       3        three
//     [2]:       2        two
//     [3]:       1        one
//     [4]:       0        zero
// 
// The key "2" is in the Hashtable.
// The key "6" is NOT in the Hashtable.
// The value "three" is in the Hashtable.
// The value "nine" is NOT in the Hashtable. 

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Hashtable Class | Hashtable Members | System.Collections Namespace | Contains | ContainsValue

Show:
© 2015 Microsoft