This documentation is archived and is not being maintained.

Hashtable.Add Method

Adds an element with the specified key and value into the Hashtable.

[Visual Basic]
Public Overridable Sub Add( _
   ByVal key As Object, _
   ByVal value As Object _
) Implements IDictionary.Add
[C#]
public virtual void Add(
 object key,
 object value
);
[C++]
public: virtual void Add(
 Object* key,
 Object* value
);
[JScript]
public function Add(
   key : Object,
 value : Object
);

Parameters

key
The key of the element to add.
value
The value of the element to add. The value can be a null reference (Nothing in Visual Basic).

Implements

IDictionary.Add

Exceptions

Exception Type Condition
ArgumentNullException key is a null reference (Nothing in Visual Basic).
ArgumentException An element with the same key already exists in the Hashtable.
NotSupportedException The Hashtable is read-only.

-or-

The Hashtable has a fixed size.

Remarks

A key cannot be a null reference (Nothing in Visual Basic), but a value can be.

An object that has no correlation between its state and its hash code value should typically not be used as the key. For example, String objects are better than StringBuilder objects for use as keys.

The Item property can also be used to add new elements by setting the value of a key that does not exist in the Hashtable. For example: myCollection["myNonexistentKey"] = myValue. However, if the specified key already exists in the Hashtable, setting the Item property overwrites the old value. In contrast, the Add method does not modify existing elements.

Example

The following example shows how to add elements to the Hashtable.

[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("one", "The")
        myHT.Add("two", "quick")
        myHT.Add("three", "brown")
        myHT.Add("four", "fox")
        
        ' Displays the Hashtable.
        Console.WriteLine("The Hashtable contains the following:")
        PrintKeysAndValues(myHT)
    End Sub
    
    Public Shared Sub PrintKeysAndValues(myList As Hashtable)
        Dim myEnumerator As IDictionaryEnumerator = myList.GetEnumerator()
        Console.WriteLine(ControlChars.Tab + "-KEY-" + ControlChars.Tab _
           + "-VALUE-")
        While myEnumerator.MoveNext()
            Console.WriteLine(ControlChars.Tab + "{0}:" + ControlChars.Tab _
               + "{1}", myEnumerator.Key, myEnumerator.Value)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' The Hashtable contains the following:
'     -KEY-    -VALUE-
'     three:    brown
'     four:    fox
'     two:    quick
'     one:    The 

[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( "one", "The" );
      myHT.Add( "two", "quick" );
      myHT.Add( "three", "brown" );
      myHT.Add( "four", "fox" );

      // Displays the Hashtable.
      Console.WriteLine( "The Hashtable contains the following:" );
      PrintKeysAndValues( myHT );
   }


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

The Hashtable contains the following:
    -KEY-    -VALUE-
    three:    brown
    four:    fox
    two:    quick
    one:    The
*/ 

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

using namespace System;
using namespace System::Collections;


void PrintKeysAndValues( Hashtable* myList )  {
   IDictionaryEnumerator* myEnumerator = myList->GetEnumerator();
   Console::WriteLine( S"\t-KEY-\t-VALUE-" );
   while ( myEnumerator->MoveNext() )
      Console::WriteLine( S"\t{0}:\t{1}", myEnumerator->Key, myEnumerator->Value );
   Console::WriteLine();
}

void main()  {
   // Creates and initializes a new Hashtable.
   Hashtable* myHT = new Hashtable();
   myHT->Add( S"one", S"The" );
   myHT->Add( S"two", S"quick" );
   myHT->Add( S"three", S"brown" );
   myHT->Add( S"four", S"fox" );

   // Displays the Hashtable.
   Console::WriteLine( S"The Hashtable contains the following:" );
   PrintKeysAndValues( myHT );
}

 /*
 This code produces the following output.

 The Hashtable contains the following:
     -KEY-    -VALUE-
     three:    brown
     four:    fox
     two:    quick
     one:    The
 */

[JScript] 
import System
import System.Collections

// Creates and initializes a new Hashtable.
var myHT : Hashtable = new Hashtable()
myHT.Add("one", "The")
myHT.Add("two", "quick")
myHT.Add("three", "brown")
myHT.Add("four", "fox")

// Displays the Hashtable.
Console.WriteLine("The Hashtable contains the following:")
PrintKeysAndValues(myHT)
    
function PrintKeysAndValues(myList : Hashtable){
    var myEnumerator : IDictionaryEnumerator = myList.GetEnumerator()
    Console.WriteLine("\t-KEY-\t-VALUE-")
    while(myEnumerator.MoveNext())
        Console.WriteLine("\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value)
    Console.WriteLine()
}

// This code produces the following output.
// 
// The Hashtable contains the following:
//     -KEY-    -VALUE-
//     three:   brown
//     four:    fox
//     two:     quick
//     one:     The 

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 | Remove | Item | IDictionary.Add

Show: