This article is a mirror article of machine translation, please click here to jump to the original article.

View: 18920|Reply: 0

[Source] Clone shallow replication of c# objects

[Copy link]
Posted on 7/1/2016 10:52:47 AM | | | |


Clone is divided into shallow and deep copies

The difference between the two: When there is a reference type member, the shallow copy copies the member's reference, and the deep copy copies the member object.

How to implement the copy function of objects:

== Inheriting the interface ICloneable implements the Clone method

== ICloneable.Clone itself does not distinguish between Deep or Shallow, you can call MemberwiseClone in Clone to implement a ShallowClone, or you can implement a DeepClone yourself. However, according to Microsoft's recommendation, when implementing ICloneable, it is ready to implement a DeepClone.
== ICloneable.Clone returns the type of object
== The replication principle of MemberwiseClone is that the value type is copied bitwise, and the reference type copies the reference of the object. One thing to note here is the String type, although it is a reference type, but here it is the same as the value type, and it is treated as a value type in Clone.









Previous:a different object with the same identifier value was already associated with...
Next:mvc fetches the JSON XML data for the post
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com