Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Hashtable vs. Dictionary

Hashtable vs. Dictionary

Scheduled Pinned Locked Moved C#
visual-studioquestion
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    honeyman_can
    wrote on last edited by
    #1

    when is it appropriate to use a Dictionary and a Hashtable? Dictionary cacheLanguage = new Dictionary(); Hashtable table = new Hashtable();

    E N I L 4 Replies Last reply
    0
    • H honeyman_can

      when is it appropriate to use a Dictionary and a Hashtable? Dictionary cacheLanguage = new Dictionary(); Hashtable table = new Hashtable();

      E Offline
      E Offline
      eggsovereasy
      wrote on last edited by
      #2

      a Dictionary is a generic collection where as a Hashtable is just a collection. So if you need to use generics use a Dictionary, if you don't use a Hashtable. Otherwise, they are identical, think of a Dictionary as a Hashtable for generics, just like ArrayList and List. btw, should be: Dictionary<typeOfKey, typeOfValue> cacheLanguage = new Dictionary<typeOfKey, typeOfValue>(); -- modified at 14:53 Monday 24th July, 2006

      1 Reply Last reply
      0
      • H honeyman_can

        when is it appropriate to use a Dictionary and a Hashtable? Dictionary cacheLanguage = new Dictionary(); Hashtable table = new Hashtable();

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        Because a HashTable uses object for both the key and value it is possible to do soemthing like this Hashtable ht = new Hashtable(); ht.Add("one", 1); ht.Add(1, 1); Not very type safe and it incurs the boxing/unboxing penalty. Where as the Dictionary uses the specified types for key and value the above would not be possible (unless you used object as the key)

        only two letters away from being an asset

        H L 2 Replies Last reply
        0
        • N Not Active

          Because a HashTable uses object for both the key and value it is possible to do soemthing like this Hashtable ht = new Hashtable(); ht.Add("one", 1); ht.Add(1, 1); Not very type safe and it incurs the boxing/unboxing penalty. Where as the Dictionary uses the specified types for key and value the above would not be possible (unless you used object as the key)

          only two letters away from being an asset

          H Offline
          H Offline
          honeyman_can
          wrote on last edited by
          #4

          So basically the two does exactly the same thing, but Dictionary is just type safe? Does a Dictionary also hash the key when storing key/value pairs? They say Hashtables are optimized for fast retrievals and that is what I need for a multi-lingual website.

          1 Reply Last reply
          0
          • N Not Active

            Because a HashTable uses object for both the key and value it is possible to do soemthing like this Hashtable ht = new Hashtable(); ht.Add("one", 1); ht.Add(1, 1); Not very type safe and it incurs the boxing/unboxing penalty. Where as the Dictionary uses the specified types for key and value the above would not be possible (unless you used object as the key)

            only two letters away from being an asset

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            Mark Nischalke wrote:

            ht.Add("one", 1); ht.Add(1, 1);

            :wtf: ;P

            **

            xacc.ide-0.2.0 preview - Now in 100% C# goodness

            **

            1 Reply Last reply
            0
            • H honeyman_can

              when is it appropriate to use a Dictionary and a Hashtable? Dictionary cacheLanguage = new Dictionary(); Hashtable table = new Hashtable();

              I Offline
              I Offline
              Ista
              wrote on last edited by
              #6

              If you have very few items a hashtable is the fastest. A Dictionary is still a key value pair and boxes unless you use a generic. Dictionaries are easier to use than a hashtable. But, since you will be storing many words or something, then a dictionary would be more useful to store unique sets. But, then again thats why globalization namespace was made.

              1 line of code equals many bugs. So don't write any!!

              1 Reply Last reply
              0
              • H honeyman_can

                when is it appropriate to use a Dictionary and a Hashtable? Dictionary cacheLanguage = new Dictionary(); Hashtable table = new Hashtable();

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Dictionary class is implemented as a hash table and it's a generic class and performance wise it's close to O(1). Therefore I'm not sure why would one use hash table unless for backward compatability. More here: ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref2/html/T_System_Collections_Generic_Dictionary`2.htm Regards.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups