Microsoft Visual C#.NET 2003 Kick Start
Dictionaries are collections that let you store values that are indexed by keys , not index numbers . For example, say you wanted to create a data record for each of your customers. You could store each customer's record using an array of values, but then you have to remember that index 0 in the array holds the customer's name , index 2 holds the address, index 3 the phone number, and so on. It is easier to index those values with text string keys like "Name" , "Address" , "Phone" , and so on, and that's what dictionaries let you do. In fact, in .NET, you can use any kind of keyan integer, object, float, string, and so onwith any kind of valuean integer, object, float, string, and so on. Hash tables are dictionaries that are highly optimized for fast data retrieval. You can see the significant public properties of Hashtable objects in Table 6.11, and their significant methods in Table 6.12. Table 6.11. Significant Public Properties of Hashtable Objects
Table 6.12. Significant Public Methods of Hashtable Objects
Like other dictionary objects, hash tables implement the IDictionary interface, which supports the public property Item to access values; you can also reach this property with the [] operator. That means you can access a value in a hash table like this: value = hashtable [ key ]; . Here's an example, ch06_09.cs, in Listing 6.9. This code starts by creating a new hash table named customer and adds the value "Cary Grant" under the key "Name" , the value "1313 Mockingbird Lane" under the key "Address" , and so on. Then we access the customer's name using the "Name" key like so:
Hashtable customer = new Hashtable(); customer.Add("Name", "Cary Grant"); customer.Add("Address", "1313 Mockingbird Lane"); customer.Add("Phone", "555.1212"); customer.Add("ID", "12345"); System.Console.WriteLine("customer[\"Name\"] = {0}", customer["Name"]); You can also get a collection of the keys from a hash table using the Keys property, which returns an ICollection object containing the keys in the hash table. You can also get the values from a hash table using the Values property, which returns an ICollection containing the values in the hash table. Here's how you can loop over the keys and values from the hash table:
ICollection keys = customer.Keys; ICollection values = customer.Values; System.Console.WriteLine(); System.Console.WriteLine("Keys"); foreach(string key in keys) { System.Console.WriteLine("{0} ", key); } System.Console.WriteLine(); System.Console.WriteLine("Values:"); foreach (string value in values) { System.Console.WriteLine("{0} ", value); } You can see the entire example, ch06_09.cs, in Listing 6.9, where we store values in a hash table, retrieve values, and loop over both the keys and values. Listing 6.9 Creating a Hash Table (ch06_09.cs)
using System.Collections; public class ch06_09 { static void Main() { Hashtable customer = new Hashtable(); customer.Add("Name", "Cary Grant"); customer.Add("Address", "1313 Mockingbird Lane"); customer.Add("Phone", "555.1212"); customer.Add("ID", "12345"); System.Console.WriteLine("customer[\"Name\"] = {0}", customer["Name"]); ICollection keys = customer.Keys; ICollection values = customer.Values; System.Console.WriteLine(); System.Console.WriteLine("Keys:"); foreach(string key in keys) { System.Console.WriteLine("{0} ", key); } System.Console.WriteLine(); System.Console.WriteLine("Values:"); foreach (string value in values) { System.Console.WriteLine("{0} ", value); } } } Here's what you see when you run ch06_09:
C:\>ch06_09 customer["Name"] = Cary Grant Keys: ID Name Address Phone Values: 12345 Cary Grant 1313 Mockingbird Lane 555.1212 |