A brief discussion on shallow copy and deep copy in .Net

青灯夜游
Release: 2019-01-29 15:30:20
Original
4125 people have browsed it

Object copying is the process of copying the properties of a data object to another object of the same data type. In .Net shallow copy and deep copy are used to copy data between objects. The following article will give you a brief understanding of shallow copy and deep copy in .Net. I hope it will be helpful to you.

A brief discussion on shallow copy and deep copy in .Net

What is a shallow copy?

Shallow copy creates a new object and then copies the non-static fields of the current object to the new object. If the field is a value type, a bit-by-bit copy of the field is performed. If the field is a reference type, the unique reference is copied, but the referenced object itself is not copied; therefore the original and clone refer to the same object. [Video tutorial recommendation: ASP.NET Tutorial]

A brief discussion on shallow copy and deep copy in .Net

A shallow copy of an object is a new object whose instance variables are the same as the old object. In .Net shallow copying is done by the object method MemberwiseClone().

If you have an object with a value and you want to create a copy of the object in another variable of the same type, then you can use shallow copy and all the attribute values ​​belonging to the value type will be Copy, but if you have a property of reference type, this instance will not be copied, only the instance will be referenced.

What is a deep copy?

Deep copy is the process of creating a new object and then copying the fields of the current object to the newly created object to create a complete copy of the internal reference type. If the field is a value type, a bit-by-bit copy of the field is performed. If the field is a reference type, a new copy of the reference object is made.

A brief discussion on shallow copy and deep copy in .Net

#A deep copy of an object is a new object with completely new instance variables, it does not share objects with the old object. When performing Deep Copy, the class to be cloned must be marked [Serializable].

Deep copy aims to copy all elements of an object, including value type elements that are directly referenced and indirect reference elements that are reference types that contain a reference to a memory location that contains the data rather than the data itself.

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of A brief discussion on shallow copy and deep copy in .Net. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template