by FransBouma via Frans Bouma's blog on 5/2/2006 2:52:00 PM
(I used the VB.NET notation in the title so it won't nag about illegal characters like < and >) In .NET 2.0 you'll have this new generic Hashtable variant called Dictionary<TKey, TValue>. 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];
What's often overlooked is a performance penalty with this pattern shift. Because of the exception, people will do something like this:
MyClass toReturn; if(!_myDictionary.ContainsKey(key)) { toReturn = _myDictionary[key]; } else { toReturn = new MyClass(); }
MyClass toReturn = null; if(!_myDictionary.TryGetValue(key, out toReturn)) { toReturn = new MyClass(); }
About the lack of blogging lately: I was very busy with finalizing the beta for LLBLGen Pro v2.0, which first beta was released 2 days ago. It's still pretty hectic, but I hope to have more time soon to blog more!
Original Post: (.NET 2.) Dictionary(Of Key, Value) performance trick
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.