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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:14:19+00:00 2026-06-06T17:14:19+00:00

I have a member variable which is a list of reference types. In a

  • 0

I have a member variable which is a list of reference types. In a method I create and item, add it to the list. The item is then updated point to another instance but the instance in the list does not get updated (his is in some unit test code). Example looks like

Item localItem = new Item(arg1, arg2);
this.itemList.Add(localItem);

// Do some testing and assertions

localItem = new Item(arg3, arg4);  // This does not update the instance of
                                   // localItem in this.ItemList

// Do some more testing and assertions

I can update my tests to fix this but it still caught me by surprise. I supose the List wants to keep the original instance passed through the Add method and cares not if the local variable which was used to pass it in now points to something else. Can anyone maybe confirm this understanding or explain it more clearly?

  • 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-06T17:14:21+00:00Added an answer on June 6, 2026 at 5:14 pm

    Ofcourse not. The localItem is a reference to the actual object (not the object itself). The list contains also a reference to this same object. If you assign another item to localItem it now points to another object but the list has still a reference to the old object.

    If you want the object in the list to update automatically you must introduce a new object that holds a reference to the item and add this one to the list

    public class ItemReference
    {
        public Item Item { get; set; }
    }
    

    Now you can create a local item like this

    ItemReference itemRef = new ItemReference();
    itemRef.Item = new Item(arg1, arg2);
    
    this.itemReferenceList.Add(itemRef);
    itemRef.Item = new Item(arg3, arg4);
    

    Because the list now has a reference to the same (unchanged) itemRef it “sees” the new item as well.

    The list has to be declared as

    List<ItemReference> itemReferenceList;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scenario : I have a member variable, an array-list,(which is not static, private member)
I have a class that has a vector member variable, which I fill up
The goal is to have the member variable _AddValue point to the CreateFirstValue function
I have a private member std::list<MyClass*> myclass_list; which is part of OtherClass. When i
Is it possible in C# to have a Struct with a member variable which
I have a member variable that in a class that I want to make
I have a member variable that tells units for a value I have measured
let's say I have std::map< std::string, std::string > m_someMap as a private member variable
I have a DataGridView to which I've set a list of objects to the
I have list in SML, which contains members from datatype expr (list b). 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.