The code sample in C# 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("US", "UNITED STATE"); ohash.Add("IN", "INDIA"); ohash.Add("PAK", "PAKISTAN");
//value are stored in key and value pair in hashtable //to fetch the value we use key name, for example string countryName = ohash["US"].ToString();
//to delete any record, we will also use key name, for example ohash.Remove("US");
//now to fetch all the values from hashtable IDictionaryEnumerator oIDic = ohash.GetEnumerator(); while (oIDic.MoveNext()) { Response.Write(oIDic.Value.ToString()); }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|