Skip to main content

Timeline for answer to Copy constructor versus Clone() by Simon P Stevens

Current License: CC BY-SA 4.0

Post Revisions

15 events
when toggle format what by license comment
Mar 24, 2023 at 12:11 history edited Glorfindel CC BY-SA 4.0
broken link fixed, cf. https://meta.stackoverflow.com/a/406565/4751173
Jul 4, 2016 at 0:00 comment added goodeye Great answer to extend long-ago posts about implementing ICloneable. But with T DeepClone(); I get this warning 'IDeepCloneable<T>.DeepClone()' hides inherited member 'IDeepCloneable.DeepClone()'. Use the new keyword if hiding was intended. I think it needs to be new T DeepClone(); as in stackoverflow.com/a/3570703/292060 and stackoverflow.com/a/21203281/292060
Feb 23, 2015 at 17:12 comment added Joel McBeth I don't think a cloneable interface that is generic or uses object makes sense. I just can't see too many uses where you would need this to be an interface instead of just adding it directly to the type you are wanting to copy.
Sep 18, 2014 at 19:38 comment added Johnny_D And what about method parameters? How they are copied, if I didn't implemented any interface?
Jul 3, 2014 at 10:36 comment added Simon P Stevens @Kyle say you had a method that took cloneable objects, MyFunc(IDeepClonable data), then it could work on all clonables, not just a specific type. Or if you had a collection of cloneables. IEnumerable<IDeepClonable> lotsOfCloneables then you could clone lots of objects at the same time. If you don't need that kind of thing though, then leave the non-generic one out.
Jun 26, 2014 at 8:20 comment added Kyle Baran Question! In what situation would you ever want the non generic version? To me, it makes sense for only IDeepCloneable<T> to exist, because ... you do know what T if you make your own implementation, ie SomeClass : IDeepCloneable<SomeClass> { ... }
S May 11, 2012 at 11:21 history suggested CommunityBot CC BY-SA 3.0
//explicit interface implementation can't have and access modifier
May 11, 2012 at 6:44 review Suggested edits
S May 11, 2012 at 11:21
Jul 28, 2010 at 20:30 comment added Simon P Stevens @abatishchev: There are two interfaces. The explicit interface implementation is for the non-generic DeepClone method that returns just an object. The implicit one is for the generic interface so can be used when the exact type is known.
Jul 28, 2010 at 14:51 comment added supercat @supercat: The dummy parameter would exist precisely to allow type inference. There are situations where some code might want to clone something without having ready access to what the type is (e.g. because it's a field, property, or function return from another class) and a dummy parameter would allow a means of properly inferring the type. Thinking about it, it's probably not really helpful since the whole point of an interface would be to create something like a deep-cloneable collection, in which case the type should be the collections' generic type.
Jul 28, 2010 at 10:38 history edited Simon P Stevens CC BY-SA 2.5
minor grammer improvement
Jul 28, 2010 at 9:51 comment added Simon P Stevens @supercat: Are you saying have a dummy parameter so the type can be inferred? It's an option I suppose. I'm not sure I like having a dummy parameter just to get the type inferred automatically. Perhaps I'm misunderstanding you. (Maybe post some code in a new answer so I can see what you mean).
Jul 27, 2010 at 17:50 comment added supercat If you're going for an interface, how about having DeepClone(of T)() and DeepClone(of T)(dummy as T), both of which return T? The latter syntax would allow T to be inferred based upon the argument.
Jul 27, 2010 at 16:19 history edited Simon P Stevens CC BY-SA 2.5
added 788 characters in body; added 38 characters in body; deleted 79 characters in body
Jul 27, 2010 at 16:00 history answered Simon P Stevens CC BY-SA 2.5