How to manually create a deep copy

After this site: http://www.csharp411.com/c-object-clone-wars/

I decided to manually create a deep copy of my class (next site 1. Clone Manually). I implemented the clone interface and any necessary properties. I ran my program and checked if my clone was really the same source instance. That was right.

However, my new instance still refers to the original. Thus, any changes in my copy that are reflected in the original instance.

So, if this does not create a deep copy, then what? What could have gone wrong?

(I want to make a deep copy manually to increase my productivity, so I don’t want to use the ObjectCopier class (even if it works fine, it takes 90% of the time of my code).

Code snippets:

The class implements:

public class SudokuAlgorithmNorvig: ICloneable
{

Cloning Method:

    public object Clone()
    {
        SudokuAlgorithmNorvig sudokuClone = new SudokuAlgorithmNorvig(this.BlockRows, this.BlockColumns);

        sudokuClone.IsSucces = this.IsSucces;

        if (this.Grid != null) sudokuClone.Grid = (Field[,])this.Grid;
        if (this.Peers != null) sudokuClone.Peers = (Hashtable)this.Peers;
        if (this.Units != null) sudokuClone.Units = (Hashtable)this.Units;

        return sudokuClone;
    }

The clone method call:

SudokuAlgorithmNorvig sudokuCopy = (SudokuAlgorithmNorvig)sudoku.Clone()

I did the same thing (clone deployment and installation method) in all my other classes. ( Field+ Coordinate)

+5
source share
1 answer

It looks like you are creating references to existing objects everywhere instead of making copies.

BlockRows BlockColumns , ? BlockRows BlockColumns , .

, Grid, Peers Units, , , . . Grid SudokuAlgorithmNorvig Grid .

+3

All Articles