Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 407575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:36:29+00:00 2026-05-12T17:36:29+00:00

I wish to implement a deepcopy of my classes hierarchy in C# public Class

  • 0

I wish to implement a deepcopy of my classes hierarchy in C#

public Class ParentObj : ICloneable
{
    protected int   myA;
    public virtual Object Clone ()
        {
             ParentObj newObj = new ParentObj();
             newObj.myA = theObj.MyA;
             return newObj;
        }
}

public Class ChildObj : ParentObj
{
    protected int   myB;
    public override Object Clone ( )
        {
             Parent newObj = this.base.Clone();
             newObj.myB = theObj.MyB;

             return newObj;
        }
}

This will not work as when Cloning the Child only a parent is new-ed. In my code some classes have large hierarchies.

What is the recommended way of doing this? Cloning everything at each level without calling the base class seems wrong? There must be some neat solutions to this problem, what are they?

Can I thank everyone for their answers. It was really interesting to see some of the approaches. I think it would be good if someone gave an example of a reflection answer for completeness. +1 awaiting!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:36:29+00:00Added an answer on May 12, 2026 at 5:36 pm

    The typical approach is to use “copy constructor” pattern a la C++:

     class Base : ICloneable
     { 
         int x;
    
         protected Base(Base other)
         {
             x = other.x;
         }
    
         public virtual object Clone()
         {
             return new Base(this);
         }
     }
    
     class Derived : Base
     { 
         int y;
    
         protected Derived(Derived other)
              : Base(other)
         {
             y = other.y;
         }
    
         public override object Clone()
         {
             return new Derived(this);
         }
     }
    

    The other approach is to use Object.MemberwiseClone in the implementation of Clone – this will ensure that result is always of the correct type, and will allow overrides to extend:

     class Base : ICloneable
     { 
         List<int> xs;
    
         public virtual object Clone()
         {
             Base result = this.MemberwiseClone();
    
             // xs points to same List object here, but we want
             // a new List object with copy of data
             result.xs = new List<int>(xs);
    
             return result;
         }
     }
    
     class Derived : Base
     { 
         List<int> ys;
    
         public override object Clone()
         {
             // Cast is legal, because MemberwiseClone() will use the
             // actual type of the object to instantiate the copy.
             Derived result = (Derived)base.Clone();
    
             // ys points to same List object here, but we want
             // a new List object with copy of data
             result.ys = new List<int>(ys);
    
             return result;
         }
     }
    

    Both approaches require that all classes in the hierarchy follow the pattern. Which one to use is a matter of preference.

    If you just have any random class implementing ICloneable with no guarantees on implementation (aside from following the documented semantics of ICloneable), there’s no way to extend it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish to generate (emit) class that implement an Interface at run-time with C#.
I wish to implement a 2d bit map class in Python. The class would
I have a class Polygon on which I wish to implement two iterators: one
I wish to implement multi-class SVM using one-vs-one method on fisheriris data in MATLAB.
I wish to implement SQL Server Profiler in a C#/ VB.NET application. Is there a
I wish to implement soft shadows produced by area lights in my raytracer. I'm
I'm working on a project in C++ where I'd wish to implement the ability
I wish HTML could do something semantically equivalent to this; <dl class=main-list> <definitionitem> <dt>Some
I wish to implement a "Last Observation Carried Forward" for a data set I
Starting iOS development, I wish to implement handwriting recognition in my app. I did

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.