© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
85 replies
Waffen

❔ Deep clone variable

How to create a complete, isolated copy of a variable to prevent the copy from changing when changed parent variable.

I have tried some variants:
1. The most easiest variant
var parent = new Class();
var clonedParent = parent;
// do some changes with parent
if (parent == clonedParent) <-- True
var parent = new Class();
var clonedParent = parent;
// do some changes with parent
if (parent == clonedParent) <-- True


2. Variant with
ICloneable
ICloneable
*logic still not changed
class Person : ICloneable
{
    public string Name { get; set; }
    public int Age { get; set; }

    public object Clone()
    {
        return new Person { Name = this.Name, Age = this.Age };
    }
}
class Person : ICloneable
{
    public string Name { get; set; }
    public int Age { get; set; }

    public object Clone()
    {
        return new Person { Name = this.Name, Age = this.Age };
    }
}

if (parent == clonedParent) <-- True
if (parent == clonedParent) <-- True
Still True

3. A little bit harder variant with
ICloneable
ICloneable

class myClass : ICloneable
{
    public string Name { get; set; }
    public int Age { get; set; }

    public object Clone()
    {
        return this.MemberwiseClone();
    }
}
class myClass : ICloneable
{
    public string Name { get; set; }
    public int Age { get; set; }

    public object Clone()
    {
        return this.MemberwiseClone();
    }
}

if (parent == clonedParent) <-- True
if (parent == clonedParent) <-- True
Still True

Do you know any other variants to clone and isolate variable?harold
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

Clone
C#CC# / help
2y ago
clone
C#CC# / help
3y ago
❔ Board Game (Marvel Champions) Gameplay Simulator - deep clone is very inefficient
C#CC# / help
3y ago
✅ Deep copy struct
C#CC# / help
13mo ago