This article mainly explains deep copy and shallow copy in C# language programming. I will explain it through an example below. Before developing an example, we must first know what deep replication is and what shallow replication is, and what the difference is. The following will be revealed one by one.
1.What is deep replication?
Deep copy is a copy of the reference type.
2. What is shallow copying?
Shallow copy is a copy of value type.
Here is a snippet of the instance code reference for deep and shallow replication in C#:
using System; using ; using ; namespace Test { public class Content { public int val; } //If it is deep replication, the ICloneable interface will be inherited. //public class Cloner : ICloneable public class Cloner { public Content MyContent = new Content(); public Cloner(int newVal) { = newVal; } //Shallow copy //Use() for shallow copying, use the getCopy method. public object getCopy() { return MemberwiseClone(); } //Deep copy: public object clone() { Cloner clonedCloner = new Cloner(); // Here is an object instantiation return clonedCloner; } } } //Main functionusing System; using ; using ; namespace Test { class Program { static void Main(string[] args) { Cloner mySource = new Cloner(5); Cloner myTarget = (Cloner)();//The depth is cloner ("={}",); = 2; ("={}", ); } } }
Through simple example development, you have a rough understanding of whether deep replication and shallow replication. I will share with you the relevant content introduction in the future