Dictionary vs. Hashtable (C# 2.0)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anders Borum

    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 KeyNotFoundExce ption 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.ContainsK ey(key)) ? this.cmsObjectN odeCache[key] : null;
    }

    // Untyped version (hashtable)
    public CmsObjectNode GetItem(Guid key)
    {
    return this.cmsObjectN odeCache[key] as CmsObjectNode;
    }

    Your comments?

    Thanks in advance!

    --
    Anders Borum / SphereWorks
    Microsoft Certified Professional (.NET MCP)


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Dictionary vs. Hashtable (C# 2.0)

    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.ContainsK ey(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]
    - mvp@spam.guard. caspershouse.co m

    "Anders Borum" <anders@spherew orks.dk> wrote in message
    news:%23TZACjr% 23FHA.1288@TK2M SFTNGP09.phx.gb l...[color=blue]
    > 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 KeyNotFoundExce ption 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.ContainsK ey(key)) ? this.cmsObjectN odeCache[key] : null;
    > }
    >
    > // Untyped version (hashtable)
    > public CmsObjectNode GetItem(Guid key)
    > {
    > return this.cmsObjectN odeCache[key] as CmsObjectNode;
    > }
    >
    > Your comments?
    >
    > Thanks in advance!
    >
    > --
    > Anders Borum / SphereWorks
    > Microsoft Certified Professional (.NET MCP)
    >[/color]


    Comment

    • Mattias Sjögren

      #3
      Re: Dictionary vs. Hashtable (C# 2.0)

      Anders,
      [color=blue]
      >However, I was surprised
      >to see that the Dictionary throws a KeyNotFoundExce ption if you try to
      >reference missing keys, which is a change from the Hashtable.[/color]

      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 cmsObjectNodeCa ch.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.

      Comment

      • Anders Borum

        #4
        Re: Dictionary vs. Hashtable (C# 2.0)

        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)


        Comment

        Working...