Deep replication and light replication in C # (clone objects in C)

Source: Internet
Author: User

Recently, we sorted generic data, but we still need to use the original data. So we copied the object to another object and forgot that the reference type is the transfer address, so there is a bug, post an articleArticle, Record your own errors.

 

C # supports two types: "value type" and "reference type ".
Value Type (such as char, Int, and float), enumeration type, and structure type.
The reference type includes the class type, interface type, delegate type, and array type.

How to divide them?
How they are allocated in computer memory

What is the difference between the value type and the reference type?
1. A Value Type Variable directly contains its data,
2. variables of the reference type store object references.
For the reference type, two variables may reference the same object. Therefore, operations on one variable may affect the objects referenced by another variable. For value types, each variable has its own data copy, and operations on one variable cannot affect another variable.

Value types are implicitly inherited from system. valuetype. Therefore, a structure cannot be displayed to inherit a class. C # does not support multi-inheritance.

A stack is an advanced, FIFO data structure. In the memory, variables are allocated to the stack for operations.
Heap is the memory area used to allocate space for Type instances (objects). It creates an object on the stack,
It will pass the object address to the variable on the stack (in turn, it is called that the variable points to this object, or the variable references this object ).

Knowledge points on the design of object cloning

Shallow copy: It means to complicate all fields in an object to a new object by words.
The Value Type field simply copies a copy to the target object. Changing the value of the Value Type field of the target object will not be reflected in the original object, because the copy is
A reference field is used to copy one of its references to the target object. Changing the value of the reference type field in the target object will be reflected in the original object, because copying is an address pointing to the heap

Deep copy: the processing of referenced fields is different from that of the shallow copy. The deep copy creates a new object and
In the original object, the corresponding field is the same (the content is the same), that is, this reference is different from the original object reference, we change
This field in the object does not affect the content of the corresponding field in the original object.

Shallow copy: to implement shallow copy, you must use the memberwiseclone method of the object class to create a superficial copy.
Deep replication: the clone method in the icloneable interface must be implemented, and the object to be cloned must be added with the [serializable] Feature

 

 

Class drawbase: system. Object, icloneable
{
Public string name = "JMJ ";
Public drawbase ()
{
}

Public object clone ()
{
Return this as object; // reference the same object
Return this. memberwiseclone (); // light copy
Return new drawbase () as object; // deep copy
}
}
Class Program
{

Static void main (string [] ARGs)
{
Drawbase rect = new drawbase ();
Console. writeline (rect. Name );
Drawbase line = rect. Clone () as drawbase;
Line. Name = "a9fs3 ";
Console. writeline (rect. Name );
Drawbase ploy = line. Clone () as drawbase;
Ploy. Name = "LJ ";
Console. writeline (rect. Name );

Console. writeline (object. referenceequals (line, Ploy ));
Console. Readline ();
}
}
Running result:
Return this as object; // reference the same object
Output: JMJ
A9fs3
LJ
True

Return this. memberwiseclone (); // light copy
Return new drawbase () as object; // deep copy
The output is JMJ.
JMJ
JMJ
False
Explanation:
The return this as object method always references the same object, so the value on the corresponding heap memory will change!
The last two methods are copying objects. The difference is that the replication type is different: Deep replication copies the entire filled object, including values of other reference types and value types of the object; the shortest copy only copies all references of an object. It copies without values and shares them with other objects that reference them.

 

Two posts Original: http://www.cnblogs.com/huangting2009/archive/2009/03/13/1410634.html
Also: http://blog.csdn.net/ifooler/archive/2007/05/06/1598452.aspx

 

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.