Concrete Example: Convert Hashtable to Dictionary (Generics)
This entry will be short and sweet. After trying to find some kind of guide for converting from Hashtables - to using the generics Dictionary...I wanted to provide a more complex yet simple code comparision.
In this example the first code snippet is the hashtable method. What this class does is store a requested url as string, and places into the hastable a object that contains all the data to dynamically create the page.
using System;
using System.Collections;
using System.Collections.Specialized;
//*********************************************************************
/// Represents a collection of PageInfo objects representing named
/// pages.
//*********************************************************************
public class NamedPageCollection : Hashtable {
//*********************************************************************
//
/// Add Method
//
/// Adds a new PageInfo object to the collection.
//
//*********************************************************************
override public void Add(Object key, Object value) {
base[key] = value;
}
//*********************************************************************
//
/// this Indexer
//
/// Returns a PageInfo object.
//
//*********************************************************************
public PageInfo this[string key] {
get {
return (PageInfo)base[key];
}
}
//*********************************************************************
/// Initializes the Hashtable as case-insensitive.
//
//*********************************************************************
public NamedPageCollection() : base(new CaseInsensitiveHashCodeProvider(), new CaseInsensitiveComparer() ) {}
}
}
Here is how you would convert the code above, to use the generics and the Dictionary<key, value> and not require changing any other code that uses this class and associated methods.
using System;
using System.Collections;
using System.Collections.Generic; //<- required
using System.Collections.Specialized;//<-maybe removed
using System.Web; //added so we can trace the output...
public class NamedPageCollection
{
public Dictionary<string, PageInfo> Dictionary = new Dictionary<string, PageInfo>();
//*********************************************************************
//
// Add Method
//
// Adds a new PageInfo object to the collection.
//
//*********************************************************************
public void Add(string key, PageInfo value)
{
Dictionary.Add(key.ToLower(), value);
HttpContext.Current.Trace.Warn("!@!@ - Key added was: <" + key+">");
}
//*********************************************************************
//
// this Indexer
//
// Returns a PageInfo object.
//
//*********************************************************************
public PageInfo this[string key]
{
get
{
try
{
return Dictionary[key.ToLower()];
}
catch
{
HttpContext.Current.Trace.Warn("!@!@ - Key requested was: <" + key + ">");
return null;
}
}
}
}
}
Note: There is no flag on the Dictionary for using case insensitive compares - hence why I convert them all to Lower(). Otherwise, if you have "/myURL.aspx" as a key but the search key is "/MYurl.aspx" - not match will be had. The try... catch is because the Dictionary will throw an exception instead of returning null (unlike the HashTable implementation)... Easy enough - catch the exception and ignore it and return a null.
In conclusion - this is a very simple case illustrating how to convert a class that inherits from the Hashtable and allows you to convert to use the generic's Dictionary<> instead....