472,796 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,796 software developers and data experts.

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
3 50457
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dick | last post by:
Is there anything that combines the features of a dictionary and a collection such that items can be retrieved by either their key or their index? I think this is how vb.6 collections worked?
4
by: Peter Rilling | last post by:
What is the difference between a hashtable and a dictionary? Why isn't Hashtable derived from DictionaryBase?
0
by: William Stacey [MVP] | last post by:
Trying to figure out Dictionary<> and using CaseInsensitive Comparer<> like I did with normal Hashtable. The Hashtable can take a case insenstive Comparer and a Case insensitive HashCode provider....
7
by: Leszek Taratuta | last post by:
Hello, I need a kind of lightweight data structure known as "associative array". It will store a few values that I need to access using textual keys. The Hashtable is too heavy for me. I also...
2
by: jg | last post by:
I was trying to get custom dictionary class that can store generic or string; So I started with the example given by the visual studio 2005 c# online help for simpledictionay object That seem...
2
by: tamara.roberson | last post by:
I am porting some code to use generics. Currently, we have a synchronized Hashtable called as follows: private Hashtable table = Hashtable.Synchronized (new Hashtable ()); What I would like...
1
by: Matt Kemmerer | last post by:
I'm trying to return a HashTable froma WebMethod. This fails with a not supported method because HashTable implements IDictionary. What I'm doing right now instead is stuffing my data into a...
3
by: wildThought | last post by:
If I have an object that contains a generic dictionary inside of it, how do I get access to its properties such as count?
18
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I've a question regarding dictionaries. I have an array elements that I created, and I'm trying to sort those elements into various sections. Here's the template of my...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.