How can you maintain the Uniqueness of a Key in a Dictionary()?

  • Technically a Dictionary requires its Keys to be unique.
  • Dictionary also uses Hashing on the Keys to ensure access to a particular key can happen in constant time.
  • If there is a complex Type (such as a custom class) we can override the GetHashCode() and Equals() method which the Dictionary internally uses to set the Key, and how two Keys can be compared while lookup.

Can you add a ComplexType as a Key in a Dictionary<T, int>()?

  • Technically a Dictionary requires its Keys to be unique.
  • Dictionary also uses Hashing on the Keys to ensure access to a particular key can happen in constant time.
  • If there is a complex Type (such as a custom class) we can override the GetHashCode() and Equals() method which the Dictionary internally uses to set the Key, and how two Keys can be compared while lookup.

Can you add a ComplexType as a Key in a Dictionary<T, int>()?


Buy Me A Coffee

Found this article helpful? Please consider supporting!

Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *