2

I was struck by how copying a collection object in C# can be quite cumbersome, especially if you want a deep copy. Is there a good design reason why .Net didn't go the Java clone() route, and is there some equivalent paradigm I've missed in C#/.Net?

Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
  • http://stackoverflow.com/questions/78536/deep-cloning-objects-in-c-sharp – Farid Movsumov Jan 10 '13 at 11:57
  • duplicate http://stackoverflow.com/questions/12733571/why-doesnt-the-net-framework-provide-a-method-to-deep-copy-objects – phnkha Jan 10 '13 at 11:58
  • 4
    Because Java's `clone()` is *not* a deep copy. – svick Jan 10 '13 at 12:00
  • @svick you learn something new every day! – Mr. Boy Jan 10 '13 at 12:06
  • 1
    @John: What is "new"? Can you ever really learn something "new"? Perhaps you can discover something new, but surely you can only ever learn something that somebody else has already discovered, which is therefore not new. Perhaps "I didn't know that!" is more suitable... don't worry, just ignore me, I am in pedantic mode! – musefan Jan 10 '13 at 12:11
  • 3
    @musefan: http://en.wikipedia.org/wiki/Idiom Ever hear of them? ;) – Matthew Watson Jan 10 '13 at 12:13
  • 1
    @MatthewWatson: Of course... do you really think I didn't understand what John meant? I was merely "pulling his leg" as one might say – musefan Jan 10 '13 at 12:20
  • 1
    More deep cloning: http://stackoverflow.com/questions/8025890/is-there-a-much-better-way-to-create-deep-and-shallow-clones-in-c/8026574#8026574 – Felix K. Jan 10 '13 at 12:20
  • @musefan it is a new piece of information stored in my memory. So I think it's accurate ;) – Mr. Boy Jan 10 '13 at 13:23
  • THis should have been closed as a duplicate, not as not constructive, otherwise many of the duplicates should be closed too! – Mr. Boy Jan 10 '13 at 13:30
  • @John: Which duplicate are you talking about? You were asking "Why doesn't .net have XYZ?". A similar question has been closed as not constructive: http://stackoverflow.com/questions/12733571/why-doesnt-the-net-framework-provide-a-method-to-deep-copy-objects. All other links here are about what deep copying in C# actually is. Those are not duplicates of your question. – Daniel Hilgarth Jan 11 '13 at 16:02
  • And another related http://stackoverflow.com/questions/1308803/why-is-cloning-in-net-so-difficult – nawfal Apr 25 '13 at 05:12

1 Answers1

7

Shallow Copies

For shallow copies, .NET offers Object.MemberwiseClone.

Deep Copies

For deep copies, Microsoft suggests to implement a custom Copy method. Deep copies require intricate knowledge about the class itself -- in fact, for a given class, it might even make sense to make multiple different types of deep copy. Thus, there is no pre-defined method or interface for this purpose.

But what about the ICloneable interface?

There is the ICloneable interface, whose purpose might correspond roughly to Java's Object.clone. However, Microsoft advises against its use, since it does not specify how shallow or deep the copy needs to be for the interface to be implemented correctly.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • 1
    Another way of doing a deep clone for a SERIALIZABLE object is to simply serialize it and then deserialize that into a new object. It's very inefficient though, but it's fine if speed isn't an issue. – Matthew Watson Jan 10 '13 at 12:16
  • @MatthewWatson It's much faster if you write your own deep copy code, see my link above. – Felix K. Jan 10 '13 at 12:25
  • Of course it's faster to write your own. That's why I said "It's very inefficient though, but it's fine if speed isn't an issue". :) – Matthew Watson Jan 10 '13 at 12:37