15

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

Mahesh Velaga
  • 21,633
  • 5
  • 37
  • 59
  • can anyone give me a example where Enumerable.Repeat can come in handy when dealing with custom objects? Thanks – Mahesh Velaga Jan 08 '10 at 17:13
  • what do you mean by "dealing with custom objects" ? – Thomas Levesque Jan 08 '10 at 17:23
  • I just want to know when is Enumerable.Repeat potentially useful – Mahesh Velaga Jan 08 '10 at 17:43
  • Mahesh, I have found Enumerable.Repeat(myCustomObject, 0).ToList() to be an effective way of creating a List, where T is an anonymous type I want to insert dynamic content into, for building up little widget classes when I am not quite ready to create a full blown class yet. – fdfrye Sep 21 '11 at 03:27

2 Answers2

15

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

-Oisin

x0n
  • 51,312
  • 7
  • 89
  • 111
8

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)

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758