Wednesday, October 27, 2010

Dictionary (.NET C#)

The System.Collections.Generic.Dictionary is incredibly easy to use. It helped me clean up some of the most excessive casting in my history as a coder. My early days of Java and C# had a lot of terrible looking code with lots of mystical casts on objects pulled from Hashtables. The haphazard casting of objects is not exactly desirable. It is a necessary evil at times, but clearly known key and value types in a Hashtable type data structure should not be one of those necessary evils.

Bitmap zBitmap = null;
string sKey = sFile.ToLower();
if (s_htImages.ContainsKey(sKey))
  zBitmap = (Bitmap)s_htImages[sKey];

No comments:

Post a Comment