Does LINQ actually perform a deep copy of the results to a different list/array/etc, or does it simply give me a list/array/etc. composed of references to the original?
Does LINQ actually perform a deep copy of the results to a different list/array/etc,
Share
It’s going to depend on if (and how) you use Select to project the results.
If you do not create new objects in a projection then the result will reference the same objects as the original collection.
If, however, you create new objects in the project then, obviously, they will not be the same.
The collection returned here will contain references to the same objects in
_myCollection:The collections returned in these cases will not:
In this case, it is worth pointing out that Prop1 and Prop2 of the new anonymous object – if they are reference types – will contain a reference to the same object as the original object. Only the top-level references in the collection will be different.
Basically – nothing in .Net aside from serializers (as mentioned elsewhere here) will “deep” copy, unless you implement it.
or
Again, it would be a mistake to assume that any “deep” copying is going on here. Of course,
Clone‘s implementation will be in the class and could be anything, including deep copying, but that is not given.