Search code examples
.neticloneable

ICloneable Vs Strongly Typed Function Vs Being Lazy


Hi Folks I need to deep clone some custom objects that references other custom objects and these may reference other cust... and so on you get the idea.

I'm just at the documentation & conception stage at the moment so wan't to get it right.

Q1. Why implement ICloneable and return an object when you could write a strongly typed custom function that returns the correct object type cloned?

Q2. The objects are not huge and I don't mind doing the initial heavy lifting copying each element but being lazy I could memberwiseClone the object and then add specific code for the referenced members again this would create a need for casting, so what is more effiecient in terms of cpu cycles?

Any thoughts, opinions & musings welcomed.


Solution

  • See 536349 for reasons not to implement ICloneable; basically you could define your own (strongly typed) interface and use that, I don't see any problems with it as long as it properly documents that it creates deep copies.