Hello!
I am using Hashtables to cache data in services and am looking at generics
for typed access (I control the key/value types). However, I was surprised
to see that the Dictionary throws a KeyNotFoundException if you try to
reference missing keys, which is a change from the Hashtable.
So what?
Well, the idea of a cache is to ensure high performance (low latency) access
to your data, and wrapping "if" statements around each "get" call sounds
expensive. I like the typed access to my cache, but dislike the overhead ..
(not sure whether the Hashtable implementation calls .ContainsKey internally
before returning null / value).
// Typed version (dictionary) - the ContainsKey method is another
// member I've implemented
public CmsObjectNode GetItem(Guid key)
{
return (this.ContainsKey(key)) ? this.cmsObjectNodeCache[key] : null;
}
// Untyped version (hashtable)
public CmsObjectNode GetItem(Guid key)
{
return this.cmsObjectNodeCache[key] as CmsObjectNode;
}
Your comments?
Thanks in advance!
--
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)