473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 50509
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.