Deep copy and shallow copy in C #

Source: Internet
Author: User
Tags shallow copy

C # supports two types: ' Value type ' and ' reference type '.

Value types (such as char, int, and float), enumeration types, and struct types.

Reference types (Reference type) include class type, interface type, delegate type, and array type.

How to divide them?

Divided by how they are allocated in the computer's memory

What is the difference between a value type and a reference type?

1, a variable of value type directly contains its data,

2, a variable of the reference type stores the object reference.

For reference types, two variables may reference the same object, so operations on one variable may affect the object referenced by another variable. For value types, each variable has its own copy of the data, and it is not possible for the operation of one variable to affect another variable.

Value type implicitly inherits from System.ValueType so it cannot be displayed. Let a struct inherit a class, C # does not support multiple inheritance

A stack is a first-in, first-out data structure in which variables are assigned to operate on the stack.

A heap (heap) is an area of memory used to allocate space for a type instance (object), creating an object on the heap.

Passes the address of an object to a variable on the stack (in turn, the variable points to this object, or the variable references the object).

About the design of object cloning to the knowledge point

Shallow copy: means to literally complicate all fields in an object to a new object

A value-type field is simply a copy of a replica to the target object, and the value of the value Type field in the target object is not reflected in the original object because the copy is copied

A reference field refers to copying one of his references to the target object. Change the value of a reference type field in the target object it will be reflected in the original object, because the copy is pointing to the heap is an address on the

Deep copy: A deep copy differs from a shallow copy in that a deep copy creates a new object in the new object and a

The same field in the original object that corresponds to the same field, which means that the reference is different from the original object, and we change the new

This field in the object does not affect the contents of the corresponding field in the original object.

Shallow copy: Implement shallow Copy the MemberwiseClone method of the object class is used to create a shallow copy

Deep replication: You must implement the Clone method in the ICloneable interface and need to clone the object plus the [Serializable] attribute

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.