What exactly happens when adding an object to a collection such as List?
List<Person> people = new List<Person>();
Person dan = new Person() { Name="daniel", Age=10 };
people.Add(dan);
dan = new Person() { Name = "hw", Age = 44 };
When I execute the above code, the List<Person> peopledoes not get affected by final line.
So I am assuming that the list copies the references of the added object, so when change the reference, the list does not get affected. am I correct?
Well, the code you've shown doesn't actually add anything to the list at all. I assume you meant to have:
people.Add(dan);
in there somewhere?
But yes, the List<T> contains references to objects. It doesn't know about where the references have come from. It's worth being clear about the difference between a variable and its value. When you call:
people.Add(dan);
that copies the value of the argument expression (dan) in this case as the initial value of the parameter within List<T>.Add. That value is just a reference - it has no association with the dan variable, other than it happened to be the value of the variable at the time when List<T>.Add was called.
It's exactly the same as the following situation:
Person p1 = new Person { Name = "Jon" };
p2 = p1;
p1 = new Person { Name = "Dan" };
Here, the value of p2 will still be a reference to the person with the name "Jon" - the second line just copies the value of p1 to p2 rather than associating the two variables together. Once you understand this, you can apply the same logic to method arguments.
Well with that code the List<Person> people should not get affected by the 2nd line either.
Anyway, the List<Person> gets a reference if you use .Add() so if you modify that person later, the person in the list is "modified" too (it's the same person), but if you assign the person to someone else you're not affecting the reference, you're assigning the symbol to a new reference.
e.g.:
List<Person> people = new List<Person>();
Person dan = new Person() { Name="daniel", Age=10 };
people.Add(dan);
/* DanData = { Name="daniel", Age=10 };
* `people[0]` maps to "DanData"
* `dan` maps to "DanData" */
dan.Name = "daniel the first";
string dansNameInList = people[0].Name; /* equals "daniel the first" */
/* DanData = { Name="daniel the first", Age=10 };
* `people[0]` maps to "DanData"
* `dan` maps to "DanData" */
people[0].Name = "daniel again";
string dansName = dan.Name /* equals "daniel again" */
/* DanData = { Name="daniel again", Age=10 };
* `people[0]` maps to "DanData"
* `dan` maps to "DanData" */
dan = new Person() { Name = "hw", Age = 44 };
string dansNameInListAfterChange = people[0].Name /* equals "daniel again" */
string dansNameAfterChange = dan.Name /* equals "hw" */
/* DanData = { Name="daniel again", Age=10 };
* NewData = { Name = "hw", Age = 44 };
* `people[0]` maps to "DanData"
* `dan` maps to "NewData" */
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With