If I use the following:
var myList = Enumerable.Repeat(myCustomObject, 2);
Will the Second element in the list be a deep copy of the first one?
Note: myCustomObject can be any Object
Edit: Could you also please let me know the potential use of Enumerable.Repeat when dealing with custom objets?
Thanks
A deep copy of an object is a copy whose properties do not share the same references (point to the same underlying values) as those of the source object from which the copy was made.
A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original. A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.
Enumerable. Repeat method is part of System. Linq namespace. It returns a collection with repeated elements in C#. Firstly, set which element you want to repeat and how many times.
No, Enumerable.Repeat actually repeats the exact same reference in the enumerable returned - it is not a copy. (verified via Reflector)
-Oisin
No, Enumerable.Repeat
will just repeat the reference, it won't make a copy of the object (unless it's a value type of course)
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