Posted on April 9, 2009 14:37 by bward

Had a little bit of time on my hands yesterday, so figured browse through reflector.  If you haven't heard of it, here is a description:

.NET Reflector enables you to easily view, navigate, and search through, the class hierarchies of .NET assemblies, even if you don't have the code for them. With it, you can decompile and analyze .NET assemblies in C#, Visual Basic, and IL.

It's a great learning tool.  Go ahead, take some time to snoop around.  You'll be surprise on what you learn!  That day, I decided to check out the HybridDictionary.  This class is a specialized collection that initially uses a ListDictionary, which is recommended for collection that contain 10 items or less, then migrates over to a HashTable

Everything that I have ever heard gave me the impression that the HybridDictionary migrates over to a HashTable once the item count is equal to or greater than ten.  This is NOT true.  Examine the following code from reflector.

  1. public void Add(object key, object value)
  2. {
  3.     if (this.hashtable != null) {
  4.         this.hashtable.Add(key, value);
  5.     }
  6.     else if (this.list == null) {
  7.         this.list = new ListDictionary(this.caseInsensitive ? StringComparer.OrdinalIgnoreCase : null);
  8.         this.list.Add(key, value);
  9.     }
  10.     else if ((this.list.Count + 1) >= 9) {
  11.         this.ChangeOver();
  12.         this.hashtable.Add(key, value);
  13.     }
  14.     else {
  15.         this.list.Add(key, value);
  16.     }
  17. }

 

If I am reading this correctly, which I like to think I am, nine is the magic number, not ten. Once the list collection reaches a count of nine, ChangeOver() is called. Here is the code for that method.

 

  1. private void ChangeOver()
  2. {
  3.     Hashtable hashtable = default(Hashtable);
  4.     IDictionaryEnumerator enumerator = this.list.GetEnumerator();
  5.     if (this.caseInsensitive) {
  6.         hashtable = new Hashtable(13, StringComparer.OrdinalIgnoreCase);
  7.     }
  8.     else {
  9.         hashtable = new Hashtable(13);
  10.     }
  11.     while (enumerator.MoveNext()) {
  12.         hashtable.Add(enumerator.Key, enumerator.Value);
  13.     }
  14.     this.hashtable = hashtable;
  15.     this.list = null;
  16. }

 

There you have it. This is a small example of what you can learn by simply downloading reflector and browsing through the code.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5