Skip to content

Instantly share code, notes, and snippets.

@kirinboy
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirinboy/1be9c1b4a241bced685d to your computer and use it in GitHub Desktop.
Save kirinboy/1be9c1b4a241bced685d to your computer and use it in GitHub Desktop.
Using a custom value type as dictionary key

Dictionary<> will take adventage of key's equality comparer when call Add, Remove, Contains and other methods. If we don't pass a comparer when declaring dictionary, it will use the default comparer. The default comparer will use key's Equals method to compare. So we have two solutions:

  1. Provide a MyKeyEqualityComparer and override its Equals(TKey x, TKey y) method.
  2. Use EqualityComparer<MyKey>.Default and override MyKey's Equals method.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment