Following this site: http://www.csharp411.com/c-object-clone-wars/
I decided to manually create a deep copy of my class (following site 1. Clone Manually). I implemented the clone interface and any necessary properties. I executed my program and checked if my clone was indeed equal too the original instance. This was correct.
However, my new instance still referenced to the original one. So any changes in my copy where reflected into the original instance.
So if this doesn't create a deep copy, then what does? What could have gone wrong?
(I want to make a deep copy manually to increase my performance, so I do not want to use the ObjectCopier class. (even if it works great, it takes 90% of my code run time).
Code Snippets:
Class implements:
public class SudokuAlgorithmNorvig: ICloneable
{
Clone 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;
}
Clone method call:
SudokuAlgorithmNorvig sudokuCopy = (SudokuAlgorithmNorvig)sudoku.Clone()
I did the same (implementing and setting clone method) in all my other classes. (Field
+ Coordinate
)