How do I know if my copy is going to be deep or shallow, when using ICloneable?

186 views Asked by At

As stated on the Microsoft's site, ICloneable interface can either deep or shallow copy:

An implementation of Clone can perform either a deep copy or a shallow copy. In a deep copy, all objects are duplicated; in a shallow copy, only the top-level objects are duplicated and the lower levels contain references.

To be honest, since there is not additional explanation on the topic, I don't understand how am I going to know when will shallow or deep copying occur?

1

There are 1 answers

2
Marc Gravell On

You can't - one of several reasons why this API is basically unusable except perhaps in your own models when you know and control everything, and in those scenarios: you can use your own clone API (perhaps even strongly typed!).

The framework does not provide any API for this scenario; you might be able to use a serializer library as a deep-clone equivalent, although that might be less efficient than you'd like.