dinsdag, mei 02, 2006

 

(.NET 2.) Dictionary(Of Key, Value) performance trick

In .NET 2.0 you'll have this new generic Hashtable variant called Dictionary. It can be used as a Hashtable but now strongly typed and it doesn't use boxing anymore with value types. That's great and all, though there's one thing that's different from the hashtable: retrieving a value with a key that's not there. If the key value isn't present in the Dictionary, you'll get an exception when you do: MyClass myValue = myDictionary[key];

MyClass toReturn = null;
if(!_myDictionary.TryGetValue(key, out toReturn)){ toReturn = new MyClass();}


ref: Frans Bouma (yep from the LLBGEN ORM)
http://weblogs.asp.net/fbouma/archive/2006/05/02/444779.aspx