Assuming I have a List<Stuff> listA that has some items in it. I create a second list as follows:
List<Stuff> listB = new List<Stuff>(listA);
Let’s say I have an item from listA, and I try to remove it from listB:
Stuff itemFromA = listA[0];
listB.Remove(itemFromA);
Assuming Stuff is a Class, should the item be successfully removed from listB? In other words, are the members the same objects or does the process of creating a new list clone the items?
I am experiencing behaviour in some code I’m debugging where the .Remove fails to remove the item from listB.
The List<T> Constructor (IEnumerable<T>) does not clone the items.
For value types the value is copied, for reference types a reference to the same object is added to the list.