About deep copies of C #

Source: Internet
Author: User

In a nutshell: 1. Shallow copy: The value of all the fields of a type is assigned once. • The value of the value type and the reference type are copied one copy, the value of the reference type is the address pointed to by the original object reference, so the modification will change the reference point of the original object to the value. (String is a special class • Used as a value type is a non-reference type)

2. Deep copy: That is, the reference type refers to the class can also be copied a copy, the modification of the point will not change the value of the original reference point.

Here's how the deep copy is implemented:

The first is the serialization method: You can inherit the ICloneable implementation of its interface clone

[serializable]//cannot serialize if Carddata is not added to the serialized identity

public class Carddata:icarddata, icloneable
{

<summary>
Deep copy
</summary>
<returns></returns>
public Object Clone ()
{
using (Stream Objectstream = new MemoryStream ())
{
IFormatter formatter = new BinaryFormatter ();
Formatter. Serialize (Objectstream, this);
Objectstream.seek (0, Seekorigin.begin);
Return formatter. Deserialize (Objectstream) as Carddata;
}
}

There's another way to write a new constructor.

Public Carddata (Icarddata card)
{

Assign all fields in turn if the field is a lot, it's very painful.

}

About deep copies of C #

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.