3

Say I have a list of Person objects (List<Person>) called persons, like this:

class Person
{
    public int PersonId { get; set; } // Unique ID for the person loaded from database
    public string Name { get; set; }
}

// In a different class
public List<Person> Persons = new List<Person>(); // Person objects are subsequently added to the list

and then I select some of the Person objects from the list, sort them by a property (e.g. by each person's unique ID PersonId), and add these selected Person objects to another list called selectedPersons.
I now need to edit the properties of a Person object that is in selectedPersons, but these changes need to be made to the original copy of this Person object in persons. I currently only know the index of this person in the selectedPersons list, so can I simply edit the person in the selectedPersons list and have the changes made to that person reflected in the original persons list, like this:

selectedPersons[selectedPersonIndex].Name = "John";

or do I have to edit the original Person object in the persons list by first finding the index of the person the persons list using the person's ID, like this:

persons[FindPersonIndexById(selectedPersons[selectedPersonIndex].BookingId).Name = "John";

where FindPersonIndexById returns the index of the person in persons whose ID matches the ID given.
I know lists are reference types in C#, but, when objects from one list are added to another list, I wasn't sure whether changes made to objects in the second list are automatically made to the same objects in the first list.

Ruben9922
  • 766
  • 1
  • 11
  • 16
  • you have 2 different instances of List... but they contain the reference to same person objects. So, any change you make on any person in any list, the same person object gets modified in both the list. – gp. Feb 28 '15 at 16:08
  • 4
    Why didn't you simply test it? – Stilgar Feb 28 '15 at 16:08
  • Such an easy few lines of code to write, why ask a question here? – DavidG Feb 28 '15 at 16:08
  • @DavidG Yeah I suppose so, though by asking on here I know exactly _why_ something works in a particular way not just _that_ it works – Ruben9922 Feb 28 '15 at 16:24
  • Does this answer your question? [Why the other list also changes if I have changed the data of one list?](https://stackoverflow.com/questions/39362251/why-the-other-list-also-changes-if-i-have-changed-the-data-of-one-list) – GSerg Jun 13 '21 at 09:56
  • @GSerg Not quite, I think that's a slightly different question. They assigned the whole list, rather than individual elements to a NEW list, so the answer to my question may not be clear from that. Note that I asked this question back when I was still learning about OOP in C# and the answer is obvious to me now. I realised it's probably something that has been asked countless times on SO. – Ruben9922 Jun 14 '21 at 12:23
  • @Ruben9922 No, they didn't. *after this sure the Property `Pincode` for the list `lm` set to `null` where name = "XYZ"... and `lm` list also set `pincode` null in the list `dst`*. – GSerg Jun 14 '21 at 12:27

2 Answers2

8

When you have a list of objects that are reference types and not value types, that is stored in the list is a reference for each object in the list. Hence if you add the same object to two different lists, you actually add to the second list the same reference. Hence if you make any change in the object, this would be "visible" from both lists.

Christos
  • 53,228
  • 8
  • 76
  • 108
3

Since Person is a reference type (class) changing name on the item in the list will change the name on the Person object and all references to that object will now see the new name. If it was a struct that would not be the case.

JasonLind
  • 370
  • 2
  • 13