467,893 Members | 1,876 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,893 developers. It's quick & easy.

Dictionary vs. Hashtable (C# 2.0)

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)
Dec 6 '05 #1
  • viewed: 50171
Share:
3 Replies
Anders,

Why not just derive from Dictionary<TKey, TValue> and then add a new
method? Something like this:

public TValue GetValue(TKey key)
{
// Check for existence here.
if (this.ContainsKey(key))
{
// Return the value.
return this[key];
}

// The key does not exist, return the default.
return default(T);
}

The problem with this is that it is valid in a Dictionary to have null
as a value corresponding to a key. The dictionary is more correct in that
sense.

You are right, using the if statement might incur an extra overhead, but
I wouldn't think that it is that much. Have you tried to use the check
against ContainsKey, and then measure the performance to see if you are
truly impacted by it?

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Anders Borum" <an****@sphereworks.dk> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
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)

Dec 6 '05 #2
Anders,
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.


It has to since TValue may be a non-nullable value type. And simply
returning default(TValue) may not always be appropriate.

But since you know that the dictionary contains CmsObjectNode objects,
you can write

public CmsObjectNode GetItem(Guid key)
{
CmsObjectNode node;
return cmsObjectNodeCach.TryGetValue(key, out node) ? node : null;
}
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 7 '05 #3
Hello!

Sorry for the long delay since my posting - life caught me.

I would like to thank you for your helpful replies. Regarding a performance
test comparing the Dictionary vs. Hashtable performance using .Contains() to
validate for the presense of keys, I am working on a small sample and will
post the findings here shortly.

Again, thanks!

--
Venlig hilsen
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)
Dec 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Dick | last post: by
4 posts views Thread by Peter Rilling | last post: by
reply views Thread by William Stacey [MVP] | last post: by
7 posts views Thread by Leszek Taratuta | last post: by
2 posts views Thread by jg | last post: by
2 posts views Thread by tamara.roberson | last post: by
1 post views Thread by Matt Kemmerer | last post: by
3 posts views Thread by wildThought | last post: by
18 posts views Thread by =?Utf-8?B?VHJlY2l1cw==?= | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.