So far, I have been using just two classes of the System.Collections.Generic namespace, List<T> and Dictionary<K,T>. At first I thought that Hashtable and Dictionary<string, T> are of the same fruit, the difference is the eliminated boxing operations when using value types for Dictionary.
I found an interesting difference this morning:
153 [Test]
154 public void HashtableIndexing()
155 {
156 Hashtable hashtable = new Hashtable();
157 object o = hashtable["jokiz"];
158 }
159
160 [Test]
161 public void DictionaryIndexing()
162 {
163 Dictionary<string, object> dictionary = new Dictionary<string, object>();
164 object o = dictionary["jokiz"];
165 }
The second test case will fail throwing an instance of System.Collections.Generic.KeyNotFoundException. There is a TryGetValue for Dictionary class if you want to be safe. One main reason for the difference is if you used valuetypes as items which of course cannot be null. More information about the differences/design decision from Brad Abrams here <sigh, that was a 2004 post>.
Posted
07-04-2007 5:59 PM
by
jokiz