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 8381703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:45:06+00:00 2026-06-09T16:45:06+00:00

To start, I have something like this: class Parent { int x; public Parent(int

  • 0

To start, I have something like this:

class Parent
{

    int x;

    public Parent(int _x)
    {
        x = _x
    }
}

class Child1: Parent
{
    int y;

    public Child1(int _y):base(_y)
    {
        y=_y;
    }
}

class Child2: Parent
{
    int z;

    public Child2(int _z):base(_z)
    {
        z=_z;
    }
}

A simple parent-child hierarchy. Then, I have a List which is actually full of Child1 and Child2. I want to make copies of each object in the list, and I want to start by making a new item that will be the copy.

But, if I do this:

foreach(Parent p in list)
dictionaryOfCopies.Add(p, new Parent(p.x));

then the dictionary will be full of Parent’s, not Children1 and Children2. Is there a way to invoke the constructor of an object that is typed as its parent type without knowing the specific type of the object?

  • 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-06-09T16:45:08+00:00Added an answer on June 9, 2026 at 4:45 pm

    One way to do it would be to implement the ICloneable interface on your objects, and let each instance clone itself.

    class Parent : ICloneable
    {
        int x;    
        public Parent(int _x)
        {
            x = _x
        }
    
        public virtual object Clone()
        {
            return new Parent(x);
        }
    }
    
    class Child1 : Parent
    {
        int y;
    
        public Child1(int _y) : base(_y)
        {
            y = _y;
        }
    
        public override object Clone()
        {
            return new Child1(y);
        }
    }
    
    class Child2 : Parent
    {
        int z;
    
        public Child2(int _z) : base(_z)
        {
            z = _z;
        }
    
        public override object Clone()
        {
            return new Child2(z);
        }
    }
    

    Then, you would use it like so:

    foreach(Parent p in list)
    {
        dictionaryOfCopies.Add(p, p.Clone() as Parent);
    }
    

    To be the devil’s advocate, one of the criticisms I’ve seen of the ICloneable interface is that it’s not type-safe. If that’s irksome to you, you can still take the same idea, but implement your own version of the Clone method that returns Parent instead of object.

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

Sidebar

Related Questions

lets say I have a class like this: public class MyClass { private String
I sometimes see that there is something like this: <input name=star2 type=radio class=star 1/>
I have three classes like this : Holiday : package mnm class Holiday {
I have something like this: import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.HashMap; import
Just want to start out by saying this seems like a great site, hope
I have an html which has tags as follows. parent <li class=pro pic notSold
Is there a way to have content blocks with the same name? base.html: This
please, what will be the outcome if i implement a code like this public
I need to have a base class which I will use to inherit other
I'm wondering if this is possible. I have html like so: <p> <font face=Georgia>

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.