DeepCloner icon indicating copy to clipboard operation
DeepCloner copied to clipboard

Cloning Dictionaries and HashSets may lead to unpredictable behavior

Open pferencgit opened this issue 4 months ago • 0 comments

I saw previous discussions about weird, unpredictable behaviors regarding Dictionaries and HashSets cloned using DeepCloner. I looked into how DeepCloner processed the cloning of such objects and I saw that the inner structure of Dictionaries and HashSets are getting cloned the same way as all other objects.

The cause of the problem is that the inner structure of these objects implement an index that rely on buckets / slots created based on hash codes of the objects stored as entries. In case the stored objects are replaced with their clones, the index may become invalid.

For example, in case of dictionaries the cloned dictionary's internal field "entries" contains hashcodes of the original objects. Therefore the function "ContainsKey" will not work correctly in the cloned dictionary.

pferencgit avatar Oct 19 '24 07:10 pferencgit