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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:20:56+00:00 2026-06-15T08:20:56+00:00

How do I copy (and insert) the last object in a list. public class

  • 0

How do I copy (and insert) the last object in a list.

public class MyObject
{
    public string first {get;set;}
    public string second {get;set;}
}

List<MyObject> list = new List<MyObject>();
list.Add(new MyObject{first = "one", second = "two"});
list.Add(new MyObject{first = "here", second = "there"});
list.Add(new MyObject{first = "car", second = "plane"});

MyObject newObj = new MyObject();
list.Add(newObj);

It seems as if the last two objects in the list is references to the same object. How do I create a new object that is still a copy of the last object in the list?

EDIT
I shouldn’t have simplified my problem and the class (not actually called MyObject) for this example. It’s actually much larger with more children objects and lists.

  • 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-15T08:20:57+00:00Added an answer on June 15, 2026 at 8:20 am

    There are two common ways:

    (1) Write a Copy Constructor for the MyObject class, and use it to make a copy of the object, then insert the copy.

    (2) Or write a Clone method for the MyObject class, and use that to make a copy, then insert the copy.

    However, in the code you presented, you are NOT making a copy of anything, nor are you inserting into the list a reference to an existing object.

    Here’s an example how you can do it:

    public class MyObject
    {
        public string first
        {
            get;
            set;
        }
        public string second
        {
            get;
            set;
        }
    
        public MyObject DeepClone()
        {
            return new MyObject{ first = this.first, second = this.second;}
        }
    }
    
    // Then you can do this:
    
    List<MyObject> list = new List<MyObject>();
    list.Add(new MyObject{first = "one", second = "two"});
    list.Add(new MyObject{first = "here", second = "there"});
    list.Add(new MyObject{first = "car", second = "plane"});
    
    list.Add(list[list.Count-1].DeepClone());        
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using INSERT INTO to copy rows of data from one table to another:
I am using mysql_query(INSERT INTO imaegs_backup SELECT * FROM images); in order to copy
in post: Copy sublist from list was stayed explained me that for copy a
I need to save a copy of an ActiveRecord object so I'm using something
Our original database has called first, middle, last for names of people. We have
I am attempting to insert a copy of a row from one table into
I am trying to insert a list of selected checkboxes into a spreadsheet, within
a linear linked list is a set of nodes. This is how a node
Suppose I have the following function: void myFunc(P& first, P& last) { std::cout <<
I get a table from the server using ajax request. After I insert the

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.