Overview
- Key-value pair based collection.
- Contains DictionaryEntry objects.
- Not accessible by index, only by key.
- Values are returned based on the hash value when doing a foreach.
- Internally uses hashes to store objects, derived from the GetHashCode method on each object.
- Hashes and Equals are used together to decide if keys are equal.
- No duplicate keys can occur.
Good practice
- When possible, use a generic collection instead of the object-based Hashtable.
- Implement GetHashCode and Equals in custom objects when you need Hashtable to treat objects as the same.
- Use a ListDictionary or HybridDictionary when working with collections of fewer than 10 items or of unknown size.
Examples
Adding and Removing Elements
Hashtable a = new Hashtable();
a.Add("Key1", "Hello");
a.Add("Key2", "World");
a.Add("Key0", "Start");
Console.WriteLine(a["Key1"] + " " + a["Key2"]);
foreach (DictionaryEntry d in a) { Console.WriteLine(d.Value); }
foreach (object v in a.Values) { Console.WriteLine(v); }
a.Remove("Key1");
foreach (object k in a.Keys) { Console.WriteLine(a[k]); }
You May Also Like

Exam 70-536 Study Notes
My study notes for Microsoft exam 70-536: .NET types, collections, generics, I/O, serialization and more, with C# examples.

Starting Exam 70-536 Study
After a burnout, I'm putting my own project on hold to study for Microsoft exam 70-536.

Design Patterns - Proxy Pattern
The Proxy Pattern, and the last article in the design patterns series, now that I've finished reading Head First Design Patterns.




