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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:40:14+00:00 2026-06-13T12:40:14+00:00

I have a List (in C#) of an object (class that I created). I

  • 0

I have a List (in C#) of an object (class that I created). I am able to use the list but the problem is that I only created (instantiated I think is the proper term) one of my object class and I keeping adding it to the list even though the properties of that object change with iterations of the code. I would like to be able to add hardcoded objects to my list and not the reference object.

Here is what I am currently doing:

public class SaveData
{
    public double save_property1;
    public double save_property2;    
}

in the application

SaveData newSaveData = new SaveData();
List<SaveData> newSaveDataList = new List<SaveData>;

if ( some condition)
{
    newSaveData.save_property1 = x
}

if (some condition 2) 
{
    newSaveData.save_property2 = y 
    newSaveDataList.Add(SaveData);  // 
}

Since X and Y change over the iterations, I want the SaveDate object I add to the list NOT to change with each iteration (so I can keep a history of the Data objects), i.e. hardcode and not use as reference.

  • 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-13T12:40:15+00:00Added an answer on June 13, 2026 at 12:40 pm

    You can’t do that, you will need to create a new object each time you want to add to the list or you will just keep modifying the old one.

    Remember that when you add a reference type to the list, you are just putting the reference into the list and not the object being referred to. This means that if you always push the same instance, all “items” in the list are really the same item. Consider:

    newSaveData                        1000
    +-----------+                      +--------------------+
    | 1000      |--------------------->| save_property1     |
    +-----------+                      | save_property2     | 
                                       +--------------------+
    newSaveDataList                               ^
    +-----------+                                 |
    | 1000      |---------------------------------+
    +-----------+                                 |
    | 1000      |---------------------------------+
    +-----------+                                 |
    | 1000      |---------------------------------+
    +-----------+                                 |
    | 1000      |---------------------------------+
    +-----------+
    

    So instead, you need to create a new instance of the object for each add to the list, so that they are all distinct objects that can vary:

    while (yourLoopCondition)
    {
        // each item you want to add, create a new instance.
        newSaveData = new SaveData();
        if (someCondition1)
        {
            newSaveData.save_property1 = x
        }
    
        if (someCondition2) 
        {
            newSaveData.save_property2 = y 
            newSaveDataList.Add(newSaveData);  
        }
    }
    

    The above assumes that each object is distinct and not an “object in flight” across multiple iterations of the loop.

    Now, if you have one main object and you’re just trying to put a “snapshot” of that object in the list, you could do this with some sort of cloning method or “copy constructor” of sorts:

    public class SaveData
    {
        public double save_property1;
        public double save_property2;
    
        public SaveData(SaveData other)
        {
            save_property1 = other.save_proerty1;
            save_property2 = other.save_property2;
        }
    }
    

    Then if you want to put a snapshot on the list of an object in flight, you could do:

    newSaveDataList.Add(new SaveData(newSaveData));
    

    Or, you could use a struct instead which is a value type and thus would place a copy in the list, but struct has a lot of interesting quirks and one should consider those very carefully before using them. I have a post on this topic here

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

Sidebar

Related Questions

I have a list of class object that I created as a variable in
I have a list of class items List<MyClass> I have a seperate object that
I have an ASP.NET WebService that returns an object of List public class Students
I have a Ticket class that contains a List of User objects: class Ticket
I have a class that holds another objects inside it (List, Set and objects
I have a class Player that contains a list of Accessory objects. There are
I have a class that has an list<Book> in it, and those Book objects
I have a class that has an list<Book> in it, and those Book objects
I have one debugger visualizer for seeing list of class object in the form
Let's say I have a generic Object class, and a generic List class. I

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.