illustrates the use of a Hashtable
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example11_6.cs illustrates the use of a Hashtable
*/
using System;
using System.Collections;
public class Example11_6
{
public static void Main()
{
// create a Hashtable object
Hashtable myHashtable = new Hashtable();
// add elements containing US state abbreviations and state
// names to myHashtable using the Add() method
myHashtable.Add("AL", "Alabama");
myHashtable.Add("CA", "California");
myHashtable.Add("FL", "Florida");
myHashtable.Add("NY", "New York");
myHashtable.Add("WY", "Wyoming");
// display the Count property
Console.WriteLine("myHashtable.Count = " + myHashtable.Count);
// lookup the state name for "CA"
string myState = (string) myHashtable["CA"];
Console.WriteLine("myState = " + myState);
// display the keys for myHashtable using the Keys property
foreach (string myKey in myHashtable.Keys)
{
Console.WriteLine("myKey = " + myKey);
}
// display the values for myHashtable using the Values property
foreach(string myValue in myHashtable.Values)
{
Console.WriteLine("myValue = " + myValue);
}
}
}
Related examples in the same category