Basic Operations Of Hashtable

The code sample explains the basic operations on a Hashtable. A Hashtable is an object that stores (key,value) pairs. With such an object, storing and searching data becomes easier.

The example describes operations such as - creation of a Hashtable, inserting data, accessing data from a hashtable and deleting values.
			
                            using System.Collections; //for hashtable
                            
                            //Create a Hashtable
                            Hashtable ohash = new Hashtable();
                            
                            //Add data using the Add() method
                            ohash.Add("AN", "ASP.NET");
                            ohash.Add("VN", "VB.NET");
                            ohash.Add("CN", "C#.NET");
                            
                            //value are stored in key and value pair in hashtable
                            //to fetch the value we use key name, for example
                            string strCategoryName = ohash["AN"].ToString();
                            
                            //to delete any record, we will also use key name, for example
                            ohash.Remove("AN");
                            
                            //now to fetch all the values from hashtable
                            IDictionaryEnumerator oIDic = ohash.GetEnumerator();
                            while (oIDic.MoveNext())
                            {
                                Response.Write(oIDic.Value.ToString());
                            }