Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Enumerable.Repeat() do a deep copy?

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

like image 599
Mahesh Velaga Avatar asked Jan 08 '10 17:01

Mahesh Velaga


People also ask

What is deep copy in JavaScript?

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.

How Objects can be copied shallow copy and deep copy?

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.

What is enumerable repeat in C#?

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.


2 Answers

No, Enumerable.Repeat actually repeats the exact same reference in the enumerable returned - it is not a copy. (verified via Reflector)

-Oisin

like image 124
x0n Avatar answered Oct 24 '22 04:10

x0n


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)

like image 30
Thomas Levesque Avatar answered Oct 24 '22 03:10

Thomas Levesque