Dictionary.ContainsKey misbehave c#

This is not a general problem but something specific to your code.

UPDATE: The hash code of the key of a dictionary must not change. This is not the case with your Column class, because as soon as any of its properties are changed, the hash code changes, too. Documentation:

As long as an object is used as a key in the Dictionary, it must not change in any way that affects its hash value.

The background of this is the following: The dictionary retrieves and stores the hash code of the key at the time it is added. If the hash code of the key changed after that, it is different to the stored hash code and the object will not be seen as equal to the originally inserted key.

If Dictionary<Column, string> is right, then I would guess it's because the type Column is not compatible with how Dictionary<TKey, TValue> checks equality - which is based on comparisons through GetHashCode and Equals - and my guess is that the Column type does not implement these.

Change the key type of the dictionary to something more suited to comparisons and equality. Ideally use a string derived from the column's name.


Based on your code update, my guess is that something about the column has changed since it went into the dictionary that causes its 'live' hash code to change from what it was when it was first added to the Dictionary - so that could be any change in Id, Name, Type, MainColumn or ChildColumns that causes their HashCodes to change.


  1. Jamir

    • 2016/6/3

    This is not a general problem but something specific to your code. UPDATE: The hash code of the key of a dictionary must not change.

  2. Mordechai

    • 2019/3/24

    Dictionary.ContainsKey misbehave c#. Ask Question Asked 10 years, 2 months ago. Active 8 years, 9 months ago. Viewed 2k times 6 1. I've got a class Column

  3. Mack

    • 2019/1/26

    Finally, it shows the least efficient way to test whether keys exist, by using the Item[] property (the indexer in C#). This code example is part of a larger 

  4. Guillermo

    • 2021/5/1

    Examples. The following code example shows how to use the ContainsKey method to test whether a key exists prior to calling the Add method. It also shows how to use the TryGetValue method to retrieve values, which is an efficient way to retrieve values when a program frequently tries keys that are not in the dictionary.

  5. Kieran

    • 2018/5/5

    Part of the English-Swedish dictionary contains translations of Folkets Lexikon. Thank you! Copyright © IDM 2021, unless otherwise noted. All rights reserved.

  6. Kieran

    • 2017/11/12

    Dictionary.ContainsKey misbehave c#. Related. 5877. How do I merge two dictionaries in a single expression (taking union of dictionaries)? 2417. Deep cloning objects

  7. Grayson

    • 2019/5/19

    This method is used to check whether the Dictionary<TKey,TValue> contains the specified key or not. Syntax: public bool ContainsKey (TKey 

  8. Gomez

    • 2018/2/24

    Syntax: public bool ContainsKey (TKey key); Here, the key is the Key which is to be located in the Dictionary. Return Value: This method will return true if the Dictionary contains an element with the specified key otherwise, it returns false. Exception: This method will give ArgumentNullException if the key is null.

  9. Justin

    • 2018/10/20

    3 synonyms of misbehaves from the Merriam-Webster Thesaurus, plus 10 related words, Find another word for misbehaves. See the Dictionary Definition.

  10. Oakley

    • 2021/4/15

    The Dictionary.ContainsKey() method in C# checks whether the Dictionary<TKey, TValue< has the specified key or not. Syntax public bool ContainsKey (TKey key); Above, the parameter key is the key to be found. Let us now see an example to implement the Dictionary.ContainsKey() method −. Example

  11. Gael

    • 2019/6/12

    noun [ C ] Any opinions in the examples do not represent the opinion of the Cambridge Dictionary editors or of Cambridge University 

  12. Bertrand

    • 2015/8/27

    ContainsKey is a Dictionary method in C# and check whether a key exists in the Dictionary or not. Declare a Dictionary and add elements −. Now, let’s say you need to check for the existence of a particular element in the Dictionary. For that, use the ContainsKey () method −. The following is the code −.

  13. Sanchez

    • 2019/4/12

    Marvell Announces Dual 400GbE MACsec PHY with Class C PTP Timestamping for Data Dec 20, 2017 · If we spend some time looking up dictionaries for the 

  14. Judah

    • 2021/5/12

    In Cocoa, NSString literals can be used as a key to a dictionary or the name of a the compiler won't complain, but your program will misbehave.

  15. Sullivan

    • 2016/11/29

    2. c . 22. Consables are to to present to the Lord Mayor and Ministers of the certify to the Rep . co an Ordinary Certificate , because it contains no .

  16. Keenan

    • 2019/10/8

    Collected and Abstracted from All Dictionaries, Abridgments, Institutes, c . without making the juftice a defendant ; then and constables , & c . are to 

Comments are closed.

Recent Posts