Hello :)
well... i need to do a simple thing (i guess) but it seems that this is not so simple due to all the complicating examples on the web...

ok. i jast wanna to create a copy of an object! but i want the copy to be seperate from the original, so when i change something in the copy it will not affect the original. in this case i need Shallow-Copy or Deep-Copy? i thought its a Deep-Copy until i saw an example that confused me...

now - how i implement it?

lets say i have this Class -

public class Position
    {
        private bool _Turn;
        private AAAA _EN;


        public bool Turn
        {
            get { return _Turn; }
            set { _Turn = value; }
        }
        public AAAA EN
        {
            get { return _EN; }
            set { _EN = value; }
        }

this is when AAAA is a class too

well, i understand that i needed to add IClonable and Clone() Methode like that:

public class Position: IClonable
    {
        private bool _Turn;
        private AAAA _EN;


        public bool Turn
        {
            get { return _Turn; }
            set { _Turn = value; }
        }
        public AAAA EN
        {
            get { return _EN; }
            set { _EN = value; }
        }

        public Position Clone()
       {
            what in here??? i got confuse...
       }

Hope u understand my question...
Thanks in advance

vedro-compota commented: + +3

Dani AI

Generated

As pointed out, your scenario needs a deep copy: every referenced object must be duplicated so mutating the copy does not change the original. Below are practical, modern options, with a short, concrete pattern you can apply immediately and a few caveats that matter in real projects.

  • Prefer a strongly typed copy constructor or a typed Clone() method on each class. This is explicit, fast and safe: copy value fields directly, allocate new collections, and call the other objects' copy constructors (or typed Clone) for reference fields. That approach avoids the ambiguity of ICloneable (Microsoft documents the interface but warns its semantics aren’t well defined) — see ICloneable docs.

  • If you need a quick generic helper, JSON serialization is a safe alternative to the old BinaryFormatter. Example patterns (note: System.Text.Json only serializes public members and needs configuration for object cycles or polymorphism):

    public static T DeepCloneJson<T>(T src)
    {
        var json = System.Text.Json.JsonSerializer.Serialize(src);
        return System.Text.Json.JsonSerializer.Deserialize<T>(json);
    }
  • Manual MemberwiseClone + manual fixes: use MemberwiseClone() for a shallow copy and then explicitly clone each reference field. Useful when most fields are value types and only a few references need special handling.

Concrete example pattern (copy ctor + typed Clone):

public class Piece
{
    public int X, Y;
    public Piece(Piece other) { X = other.X; Y = other.Y; }
}

public class GameState
{
    public bool Turn;
    public Piece[] Pieces;
    public List<int> Moves;

    public GameState(GameState other)
    {
        Turn = other.Turn;
        Pieces = other.Pieces?.Select(p => new Piece(p)).ToArray();
        Moves = other.Moves != null ? new List<int>(other.Moves) : null;
    }

    public GameState Clone() => new GameState(this);
}

Notes and cautions: mark types appropriately for the chosen serializer; avoid BinaryFormatter (obsolete and insecure — see Microsoft guidance); watch for circular references and polymorphism when serializing; and remember that an extension-method-based helper must live in a static class (that doesn’t alter your original type), as explained. For most control and correctness, use copy constructors or typed Clone() implementations for each class.

Recommended Answers

All 6 Replies

Asked and answered over here, and it's a deep copy you want :)

Thank u Monarch, but i dont understand the code in there... there are a lot of methods i dont know and i still do not use Generics yet so its not clear to me...

could you please give an example of implementing of this code?
the class ObjectCopier is the class i need to copy or its a class that just help me do that on another class?

Thank u

If you put that code into a file by itself, you'll be able to use it with any class that is serializable, doing this:

Position a = new Position();
Position b = a.Clone();

If you put that code into a file by itself, you'll be able to use it with any class that is serializable, doing this:

Position a = new Position();
Position b = a.Clone();

Thank u - one last thing.. i have notice that i need to make the class "static" for that piece of code will work...
why is that? what will happen to my class after that? it can ruin things?

thanks.. :)

It needs to be static because it is a special type of method known as an 'extension method'. It 'extends' other classes rather than being used by the class it's in.

Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.