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

  • SEARCH
  • Home
  • 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 6542859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:16:39+00:00 2026-05-25T11:16:39+00:00

I have a base class: abstract class Base and some derived classes: class Derived:

  • 0

I have a base class: abstract class Base and some derived classes: class Derived: Base, that all data members are in the base class. I have another generic list: List<Base> list. Now I want to perform the Clone() operation on the list. I’ve read this thread but found that my situation a bit more complex. Since the base class is abstract, so the elements of the list cannot be clone via copy constructor or implementing ICloneable interface. But since all data members are in base class, it will be redundant to write the same piece of code for cloning in derived classes again and again. What is the best way to do this job? Thank you for giving hints.

Update: Simplified Source Code Attached

public class Point : ICloneable
{
    public int X { get; set; }
    public int Y { get; set; }

    public Point(int x, int y)
    { X = x; Y = y; }

    public Point(Point p)
    {
        X = p.X; Y = p.Y;
    }

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

public abstract class ObjectThatHasPosition : ICloneable
{
    public Point CurrentPosition { get; set; }
    public ObjectThatHasPosition(Point p)
    { CurrentPosition = new Point(p); }
    public object Clone()
    {
        return MemberwiseClone();
    }
}

public class Man : ObjectThatHasPosition
{
    public Man(Point p) : base(p) { }
}

static class Extensions
{
    public static List<ObjectThatHasPosition> Clone(this List<ObjectThatHasPosition> src)
    {
        List<ObjectThatHasPosition> dst = new List<ObjectThatHasPosition>(src.Count);
        src.ForEach((item) => { dst.Add((ObjectThatHasPosition)item.Clone()); });
        return dst;
    }
}

    static void Main(string[] args)
    {
        List<ObjectThatHasPosition> firstList = new List<ObjectThatHasPosition>();
        firstList.Add(new Man(new Point(0, 0)));
        List<ObjectThatHasPosition> anotherList = firstList.Clone();
        firstList[0].CurrentPosition.X = 1;
    }

One can see that both lists’ element is the same.

  • 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-25T11:16:39+00:00Added an answer on May 25, 2026 at 11:16 am

    I don’t see why Base can’t implement ICloneable:

    public abstract class Base : ICloneable
    {
        public object Clone()
        {
            return MemberwiseClone();
        }
    }
    

    That will still return the right instance (i.e. the same type as the existing object) when called on a derived class. Any fields in the derived classes will be copied over too – albeit in a shallow way. As a concrete example:

    using System;
    
    public abstract class BaseClass : ICloneable
    {
        public object Clone()
        {
            return MemberwiseClone();
        }
    }
    
    public class Derived : BaseClass
    {
        private readonly string name;
    
        public Derived(string name)
        {
            this.name = name;
        }
    
        public override string ToString()
        {
            return "Derived with name " + name;
        }
    }
    
    class Test
    {
        static void Main(string[] args)
        {
            BaseClass b = new Derived("fred");
    
            object clone = b.Clone();
            Console.WriteLine(clone.ToString());
        }
    }
    

    EDIT: I suspect you want something like:

    public abstract class ObjectThatHasPosition : ICloneable
    {
        public Point CurrentPosition { get; set; }
        public ObjectThatHasPosition(Point p)
        {
            CurrentPosition = p; 
        }
    
        public object Clone()
        {
            var clone = (ObjectThatHasPosition) MemberwiseClone();
            clone.CurrentPosition = (Point) CurrentPosition.Clone();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an abstract base class that holds a Dictionary. I'd like inherited classes
I have a abstract base class A and a set of 10 derived classes.
I have a abstract base class that I have many inherited classes coming off
I have a data object which has a base class with three derived classes,
I have a stateless, abstract base class from which various concrete classes inherit. Some
I'm trying to have an abstract base class for some builder classes so I
Lets say I have an abstract base class and a derived class with some
If I have a base class such that public abstract class XMLSubscription <T extends
I have an abstract base class and derived class: type TInterfaceMethod = class public
In C#, I have a class hierarchy with a couple of abstract base classes

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.