December 2007
Intermediate to advanced
896 pages
19h 57m
English
You want to enhance the performance of your application as well as make the code easier to work with by replacing all Hashtable objects with the generic version.
Replace all occurrences of the System.Collections.Hashtable class with the type-safe generic System.Collections.Generic.Dictionary class.
Here is a simple example of using a System.Collections.Hashtable object:
public static void UseNonGenericHashtable()
{
Console.WriteLine("\r\nUseNonGenericHashtable");
// Create and populate a Hashtable
Hashtable numbers = new Hashtable()
{ {1, "one"},"one"}, // Causes a boxing operation to occur for the key
{2, "two"} }; // Causes a boxing operation to occur for the key
// Display all key/value pairs in the Hashtable
// Causes an unboxing operation to occur on each iteration for the key
foreach (DictionaryEntry de in numbers)
{
Console.WriteLine("Key: " + de.Key + "\tValue: " + de.Value);
}
Console.WriteLine(numbers.IsReadOnly);
Console.WriteLine(numbers.IsFixedSize);
Console.WriteLine(numbers.IsSynchronized);
Console.WriteLine(numbers.SyncRoot);
numbers.Clear();
}Here is that same code using a System.Collections.Generic.Dictionary<T, U> object:
public static void UseGenericDictionary() { Console.WriteLine("\r\nUseGenericDictionary"); // Create and populate a Dictionary Dictionary<int, string> numbers = new Dictionary<int, string>() { { 1, "one" }, { 2, "two" } }; // Display all key/value pairs in the Dictionary foreach ...Read now
Unlock full access