C # in the deep copy and shallow copy

The difference between a shallow vs. deep copy:

    Refers to a shallow copy of an object copied to the new object type in the numeric field, and the object type field in the reference refers to a reference copy it to a target object. If you change the value of the target object type referenced in the field he will be reflected in the original object, that is to say the original object in the corresponding field will change.

   Deep copy is different for the shallow copy processing references, deep copy will create the same field (contents) field and a new original object corresponding to the new object, and references that is the original object references are different time, we changed the new object of this field will not affect the contents of the original object in the corresponding field.

Therefore, there are two processing methods for the different modes prototype (prototype pattern): Shallow and deep copy of the copy of the object

MemberwiseClone method creates a shallow copy by creating a new object, and then copy the non-static fields of the current object to the new object. If the field is a value type, then the field is performed bit by bit copy. If the field is a reference type, then copy references but does not copy the objects referenced; therefore, a copy of the original object and reference the same object. Deep copy, i.e., can be used to implement the ICloneable interface .ICloneable deep copy and shallow copy.

provides an interface to ICloneable .net, a Clone () method in this interface, it can be achieved to realize you yourself cloning methods, such as deep or shallow clone clone, MemberwiseClone () is a method of the object class, implementation class for shallow clone

The following is a copy of the deep and shallow copy Model, for reference

  [Serializable]
    public class InvoiceDetailResponse : IDeepCopy, IShallowCopy
    {
         
        public Guid merchant_id { get; set; }
        /// <summary>
        /// 名称
        /// </summary>
        public string uname { get; set; }
       
        /// <summary>
        /// 浅拷贝
        /// </summary>
        /// <returns></returns>
        public object ShallowCopy()
        {
            return this.MemberwiseClone();
        }
        /// <summary>
        /// 深拷贝
        /// </summary>
        /// <returns></returns>
        public object DeepCopy()
        {
            BinaryFormatter bFormatter = new BinaryFormatter();
            System.IO.MemoryStream stream = new MemoryStream();
            bFormatter.Serialize(stream, this);
            stream.Seek(0, SeekOrigin.Begin);
            return (InvoiceDetailResponse)bFormatter.Deserialize(stream);
        }
    }
    /// <summary>
    /// deep copy Interface
     ///  </ Summary> 
    interface IDeepCopy 
    { 
        Object a DeepCopy (); 
    } 

    ///  <Summary> 
    /// shallow copy Interface
     ///  </ Summary> 
    interface IShallowCopy 
    { 
        Object ShallowCopy (); 
    }

 

Guess you like

Origin www.cnblogs.com/personblog/p/11308831.html