Burtsev-Alexey / net-object-deep-copy

C# extension method for fast object cloning.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inconsistent Dictionary

Rai-a opened this issue · comments

commented
using System;
using System.Collections.Generic;

public class Class1
{
    public static void Main(string[] args)
    {
        Dictionary<Class1, Class1> state1 = new Dictionary<Class1, Class1>();
        state1.Add(new Class1(), new Class1());
        foreach (Class1 key in state1.Keys)
            Console.Write(state1[key]);
        Dictionary<Class1, Class1> state2 = state1.Copy();
        foreach (Class1 key in state2.Keys)
            Console.Write(state2[key]);//Exception
    }
}

Occurs for all Dictionarys who use classes as key that do not override GetHashCode()

This is a tricky one, the only way to solve this is by filling a new Dictionary with the cloned keys and values.