C# Hashtable Add

Description

Hashtable Add adds an element with the specified key and value into the Hashtable.

Syntax

Hashtable.Add has the following syntax.


public virtual void Add(
  Object key,
  Object value
)

Parameters

Hashtable.Add has the following parameters.

  • key - The key of the element to add.
  • value - The value of the element to add. The value can be null.

Returns

Hashtable.Add method returns

Example

The following code uses Add method to add elements to the Hashtable and uses the Keys to obtain the values.


using System; //w  ww  .  ja v  a  2  s .  c  o  m
using System.Collections; 
 
class HashtableDemo { 
  public static void Main() { 
    Hashtable ht = new Hashtable(); 
     
    
    ht.Add("a", "A"); 
    ht.Add("b", "B"); 
    ht.Add("c", "C"); 
    ht.Add("e", "E"); 
 
    // Get a collection of the keys. 
    ICollection c = ht.Keys; 
 
    foreach(string str in c) 
      Console.WriteLine(str + ": " + ht[str]); 
  } 
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack