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 key type

Hashtable key type

Scheduled Pinned Locked Moved C#
5 Posts 4 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.
  • B Offline
    B Offline
    Bruce Coward
    wrote on last edited by
    #1

    Does a C# hashtable key have to be a string or could it be an int? Cheers, Bruce :confused:

    L P 2 Replies Last reply
    0
    • B Bruce Coward

      Does a C# hashtable key have to be a string or could it be an int? Cheers, Bruce :confused:

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi Bruce, a Hashtable, and since 2.0 also a generic Dictionary, does not impose limitations on the type of keys and values it takes. The key can be a string, an int, a MenuItem, whatever. It must have a decent hashing function, as documented for Dictionary< TKey, TValue>: As long as an object is used as a key in the Dictionary< (Of < (TKey, TValue> )> ), it must not change in any way that affects its hash value. Every key in a Dictionary< (Of < (TKey, TValue> )> ) must be unique according to the dictionary's equality comparer. A key cannot be nullNothingnullptra null reference (Nothing in Visual Basic), but a value can be, if the value type TValue is a reference type. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


      1 Reply Last reply
      0
      • B Bruce Coward

        Does a C# hashtable key have to be a string or could it be an int? Cheers, Bruce :confused:

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        It can be anything.

        B 1 Reply Last reply
        0
        • P PIEBALDconsult

          It can be anything.

          B Offline
          B Offline
          Bruce Coward
          wrote on last edited by
          #4

          So if I make it an int for example do I have to overide any hashing functions? In my ap the key would be a uint32 and the value an int. Cheers, Bruce :thumbsup:

          N 1 Reply Last reply
          0
          • B Bruce Coward

            So if I make it an int for example do I have to overide any hashing functions? In my ap the key would be a uint32 and the value an int. Cheers, Bruce :thumbsup:

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            Bruce Coward wrote:

            So if I make it an int for example do I have to overide any hashing functions?

            No, you don't have to. You could also consider using a generic dictionary instead of HashTable.

            Navaneeth How to use google | Ask smart questions

            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