Sunday, July 22, 2007

Hashtable: Basics: Code Listing

HashTable:
• Represents a collection of key/value pairs that are organized based on the hash code of the key
• Namespace: Systems.Collection
• Each element is a key/value stored in a DictionaryEntry object
• Value can be null, but key can not
• Objects used as keys must override the Object.GetHashCode and Object.Equals
• Key object must be immutable
• Default initial capacity of HashTable is zero
• Not safe for mutli-threaded access

Code Listing: HashTable
using System;
using System.Collections;

class Example
{
public static void Main()
{
HashTable openwith = new HashTable();
openwith.Add(“txt”,”notepad.exe”);
openwith.Add(“bmp”,”paint.exe”);

try
{
openwith.Add(“txt”,”winword.exe”);
}

catch
{
Console.Writeline(“Key \”txt\” already exists..”);
}

ICollection valuecol = openwith.Values;
foreach(string s in valuecol)
{
Console.Writeline(“Value = ” s);
}
}
}

No comments: