c# - Copy constructor versus Clone() -


In C #, what is the preferred way of copy copy functionality to a class? Can a copy constructor be implemented, or instead of ICloneable and can implement the clone () method?

Comment : I've written "deep" within the brackets because I thought it was irrelevant. Obviously, disagree with others, so I asked.

You <

The reason for this is that when Microsoft designed the net structure, they never specified that the clon () method on iclible is a deeper Or should be a shallow clone, thus the interface is meaningfully broken because your caller does not know that the call will clone the deep or shallow object.

) Interfaces with methods.

You can define two interfaces, to support a single parameter, to maintain strongly typed cloning and the ability to have a typed cloning without typing weak, when you use different types of clonal objects Working with the collection:

  Public Interface IDeepCloneable {Object DeepClone (); } Public Interface IDeepCloneable & lt; T & gt; : IDeepCloneable {T Deepclone (); }  

You will then apply as follows:

  Public class sample class: IDeepCloneable & lt; SampleClass & gt; {Public SampleClass DeepClone () {// Deep Your Object Return Clone ...; } IDeepCloneable.DeepClone () {This return. DeepClone (); }}  

Generally I like to use the spoken interface as opposed to the copy constructor, it keeps the intentions very clear. A copy maker will probably be assumed to be a deep clone, but it is certainly not as a clear intention as it is using an IDeepClonable interface.

Net and blog have been discussed on this

(I think if you are writing an application (contrary to a structure / library) then you Make sure no one outside your team will call your code, it does not make any difference and you can assign meaning to "deeply" for the real icible interface, but you should ensure that it is good Well documented and your team Within the framework guidelines, I have been well understood.)


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -