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>()?

Default image
Sriram Mannava

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity.

Leave a Reply