The Dictionary class differs from the Hashtable class in more ways than one. In addition to being strongly-typed, the Dictionary also employs a different collision resolution strategy than the Hashtable class, using a technique referred to as chaining. Recall that with probing, in the event of a collision another slot in the list of buckets is tried. (With rehashing, the hash is recomputed, and that new slot is tried.) With chaining, however, a secondary data structure is utilized to hold any collisions. Specifically, each slot in the Dictionary has an array of elements that map to that bucket. In the event of a collision, the colliding element is prepended to the bucket's list.

To better understand how chaining works, it helps to visualize the Dictionary as a hashtable whose buckets each contain a linked list of items that hash to that particular bucket. Figure 11 illustrates how series of items that hash to the same bucket will form a chain on that bucket.

 

 

using System;
using System.Collections;
//csc array_hastable_value_key.cs
namespace ex_hastable
{
public class HashtableDemo
{
private static Hashtable employees = new Hashtable();
private static Hashtable emp = new Hashtable();
// public string str1= "";
public string s2 = "";
public string sta1 = "";
public string sta2 = "";
public string sta3 = "";
public string sta4 = "";
public static int n = 0;
public static int n1 = 10;

public void set_pwd()
{
Console.WriteLine("--data set -1-- " );
Console.Write("Enter name: " );
sta2 = Console.ReadLine();
Console.Write("Enter pwd: " );
sta1 = Console.ReadLine();
//
Console.WriteLine("--data set -1-- " );
Console.Write("Enter name: " );
sta4 = Console.ReadLine();
Console.Write("Enter pwd: " );
sta3 = Console.ReadLine();
emp.Add(sta1, sta2);
emp.Add(sta3,sta4);
//sta3 = (string)emp[ss];
//Console.WriteLine("Name and pwd : "+ sta3 );

for_dic(emp);
}

public void for_dic(Hashtable ht){
foreach ( DictionaryEntry de in ht)
{ Console.WriteLine("\t{0}:\t{1}", de.Key, de.Value);


}
//return s2;
}
}
class test
{
static void Main()
{

HashtableDemo pwd = new HashtableDemo();
pwd.set_pwd();

Console.ReadLine();

}
}
}