.net 深层拷贝 浅层_.NET中的深层副本与浅层副本

.net 深层拷贝 浅层

Copy of an Object :

复制对象:

In today’s object oriented programming world, Object's are basic medium to represent real world entities. So through this practice, sometimes it becomes obvious to interchange or exchange information or even we can say, pass these objects information between different processes.

在当今的面向对象的编程世界中,对象是代表现实世界实体的基本媒介。 因此,通过这种做法,有时可以很明显地交换或交换信息,甚至可以说在不同过程之间传递这些对象信息。

Well, To perform these kind of activities It is required that we will create copy of objects and pass it using available medium so that same information will become available to another side.

好了,要执行此类活动,需要我们创建对象的副本并使用可用的介质传递它,以便相同的信息可用于另一端。

There are more than a few different ways exists to copy an object, among them we will explore two and they are Deep copy and Shallow Copy.

复制对象的方式不止几种,其中我们将探讨两种,即Deep copyShallow Copy

Shallow Copy : Shallow copy approach creates a new instance of the original object with similar nature, I mean same type, and then starts copying the original objects information or more specifically we can say its nonstatic  data members. While copying data member. It follows two different mechanism. If the data member is a value type (struct, basic data types etc), a bit-by-bit copy will be performed and If the field is a reference type(Class, collections etc), then simply reference is copied leaving referred object’s data. Therefore, As a result the reference in the original object and the reference in the copy object will point to the same object.

浅表复制:浅表复制方法将创建具有相似性质(即相同类型)的原始对象的新实例,然后开始复制原始对象信息,或更具体地说,可以说它的非静态数据成员。 在复制数据成员时。 它遵循两种不同的机制。 如果数据成员是值类型(结构,基本数据类型等),则将逐位执行复制;如果字段是引用类型(类,集合等),则仅复制引用,保留引用对象的数据。 因此,结果原始对象中的引用和复制对象中的引用将指向同一对象。

Deep copy: Deep copy approach completely interrogates the original object and copies everything irrespective of value type or reference type. Therefore a deep copy of an object will bring you, an object with all of the elements duplicated from the original object but not pointing to the source object. I mean, A deep copy of an object duplicates everything directly or indirectly referenced by the fields in the object.

深层复制:深层复制方法完全询问原始对象并复制所有内容,而与值类型或引用类型无关。 因此,对象的深层副本将带给您一个对象,该对象具有从原始对象复制的所有元素,但不指向源对象。 我的意思是,对象的深层副本将复制对象中的字段直接或间接引用的所有内容。

-Well, sometimes definitions bring us to a puzzled situation where we found ourselves bit confused. So need to get this concept step by step, I’m sure by this way we will be able to explore it more and more.

-好吧,有时定义使我们陷入困惑的境地,我们发现自己有些困惑。 因此,需要逐步理解这个概念,我相信通过这种方式,我们将能够越来越多地探索它。

Moving two steps forward with Shallow and Deep copy:

使用“浅”和“深”复制前进两个步骤:

Step 1: In first approach we will try to get it through pictures and graphs, as I think through this way it will become easy to understand it..

步骤1:在第一种方法中,我们将尝试通过图片和图形来获取它,因为我认为通过这种方式,它将变得易于理解。

-Consider two objects, A and B, which each refer to two separate memory blocks as shown in Fig - 1

-考虑两个对象A和B,它们分别引用两个单独的存储块,如图1所示。

.net 深层拷贝 浅层_.NET中的深层副本与浅层副本_第1张图片

For Shallow copy, We can say while copying A into B, Simply we are trying to attach B to the same memory block as A. Fig - 2

对于浅拷贝,我们可以说将A复制到B时,我们只是试图将B附加到与A相同的存储块上。图-2

.net 深层拷贝 浅层_.NET中的深层副本与浅层副本_第2张图片
“Be careful while getting it, as we are copying A to B, I mean neither we are trying to copy the address nor we are in the process of paasing the reference.” Simply we are in the process of copying Object A’s data into B. This results in a situation in which some data is shared between A and B as represented in Fig - 3.
.net 深层拷贝 浅层_.NET中的深层副本与浅层副本_第3张图片

For deep copy while coping A into B, we are in the process of copy data actually as shown in Fig - 4.

对于在将A转换为B时进行深度复制,实际上我们正在复制数据,如图4所示。

.net 深层拷贝 浅层_.NET中的深层副本与浅层副本_第4张图片
.net 深层拷贝 浅层_.NET中的深层副本与浅层副本_第5张图片

Step 2: In another approach we will try to get it by implementing it using C# :

步骤2:在另一种方法中,我们将尝试通过使用C#来实现它:

For the implementation, let’s create a console application “ShallowVsDeepCopyPrj” and create two separate classes Product and ProductCatalog, in both classes I have implemented ICloneable interface.

对于实现,让我们创建一个控制台应用程序“ ShallowVsDeepCopyPrj ”,并创建两个单独的类ProductProductCatalog ,在这两个类中我都实现了ICloneable接口。

namespace ShallowVsDeepCopyPrj
{
    public class Product : ICloneable
    {
        private String m_ProductName, m_ProductDesc;
        private String prodName = "ABC of C#";
        private ProductCatalog m_ProductCatalog = new ProductCatalog("ABC of C#", "AVAILABLE");
        public Product()
        {
            this.Setm_ProductDesc = "Test Your C# Skill";
            this.Setm_ProductName = prodName;
        }
        public String Setm_ProductDesc { set { m_ProductDesc = value; } get { return m_ProductDesc; } }
        public String Setm_ProductName { set { m_ProductName = value; } get { return m_ProductName; } }
        public ProductCatalog Setm_ProductCatalog { set { m_ProductCatalog = value; } get { return m_ProductCatalog; } }
        Object ICloneable.Clone() { return this.Clone(); }

        public virtual Product Clone()
        {
            // Start with a flat, Simply Memberwise copy 
            Product shallowCopy_Prod = this.MemberwiseClone() as Product;
            return shallowCopy_Prod;
        }

        public Product CloneThroughShallowCopy()
        {
            return this.Clone(); //Return Shallow Copy
        }

        public virtual Product CloneThroughDeepCopy()
        {
            // Start with a flat,Creating seperate memory block for reference type members
            Product deepCopy_Prod = new Product();

            // Then deep-copy everything that needs the - special attention
            deepCopy_Prod.Setm_ProductName = this.Setm_ProductName;
            deepCopy_Prod.Setm_ProductDesc = this.Setm_ProductDesc;
            deepCopy_Prod.Setm_ProductCatalog = this.Setm_ProductCatalog.ClonedThroughDeepCopy();

            return deepCopy_Prod;
        }
    }

    public class ProductCatalog : ICloneable
    {
        private String m_ProductName, m_ProductStatus;

        public ProductCatalog()
        {
            //Do nothing     
        }
        public ProductCatalog(string pName, string pCat)
        {
            this.Setm_ProductName = pName;
            this.Setm_ProductStatus = pCat;
        }

        public String Setm_ProductStatus { set { m_ProductStatus = value; } get { return m_ProductStatus; } }
        public String Setm_ProductName { set { m_ProductName = value; } get { return m_ProductName; } }

        Object ICloneable.Clone() { return this.Clone(); }

        public virtual ProductCatalog Clone()
        {
            // Start with new ProductCatalog object
            ProductCatalog deepCopy_ProdCat = new ProductCatalog();

            // Then deep-copy everything that needs the - special attention
            deepCopy_ProdCat.Setm_ProductName = this.Setm_ProductName;
            deepCopy_ProdCat.Setm_ProductStatus = this.Setm_ProductStatus;
            return deepCopy_ProdCat;
        }

        public ProductCatalog ClonedThroughDeepCopy()
        {
            return this.Clone();
        }
    }
}

-Now implementing these two class in main thread :

-现在在主线程中实现这两个类:

 static void Main(string[] args)
        {
            Product m_Product = new Product();
            try 
            {
                Product ShallowCopyProd = (Product)m_Product.CloneThroughShallowCopy(); // Shallow Copy of Product
                Product DeepCopyProd = (Product)m_Product.CloneThroughDeepCopy();//Deep Copy of Product

                //Making some changes to m_Product, let's se where it gets reflected and where it is not reflecting
                m_Product.Setm_ProductName = "XYZ";
                m_Product.Setm_ProductDesc = "XYZ1";
                m_Product.Setm_ProductCatalog.Setm_ProductName = "UNKNOWN";
                m_Product.Setm_ProductCatalog.Setm_ProductStatus = "NA";

                Console.WriteLine("\n");
                Console.WriteLine("Output - Shallow Copy Vs Deep Copy in .NET: ");
                Console.WriteLine("Source - Product: " + m_Product.Setm_ProductName + "/" + m_Product.Setm_ProductDesc + "/" + m_Product.Setm_ProductCatalog.Setm_ProductName + "/" + m_Product.Setm_ProductCatalog.Setm_ProductStatus);
                Console.WriteLine("Shallow Copy - Product: " + ShallowCopyProd.Setm_ProductName + "/" + ShallowCopyProd.Setm_ProductDesc + "/" + ShallowCopyProd.Setm_ProductCatalog.Setm_ProductName + "/" + ShallowCopyProd.Setm_ProductCatalog.Setm_ProductStatus);
                Console.WriteLine("Deep Copy - Product: " + DeepCopyProd.Setm_ProductName + "/" + DeepCopyProd.Setm_ProductDesc + "/" + DeepCopyProd.Setm_ProductCatalog.Setm_ProductName + "/" + DeepCopyProd.Setm_ProductCatalog.Setm_ProductStatus);
                Console.ReadLine();
            } 
            catch (Exception ex) { 
             Console.WriteLine(ex.Message.ToString()); }
        }

Will bring you output:

将为您带来输出:

Output - Shallow Copy Vs Deep Copy in .NET:

输出-浅复制与.NET中的深复制:

Source - Product: XYZ/XYZ1/UNKNOWN/NA

来源-产品:XYZ / XYZ1 / UNKNOWN / NA

Shallow Copy - Product: ABC of C#/Test Your C# Skill/UNKNOWN/NA

浅拷贝-产品:C#的ABC /测试您的C#技能/ 未知/不适用

Deep Copy - Product: ABC of C#/Test Your C# Skill/ABC of C#/AVAILABLE

深度复制-产品:C#的ABC /测试您的C#技能/ C#的ABC /可用

-By analysing bold font in the output we can say that,shallow copy reflected the change's in the source but deep copy doesn't.

As of now we are through the deep copy and shallow copy basics,I mean what they are and how they are different.

到目前为止,我们已经了解了深层复制和浅层复制的基础知识,我的意思是它们是什么以及它们之间有何不同。

Advantages / Disadvantages:

优点缺点:

The advantage of shallow copies is that their execution speed is fast and does not depend on the size of the data but data will be shared with original object while deep copies do not depends on each other (Original and deep copies) but at the cost of slower more expensive copy.

浅表副本的优点是它们的执行速度快,并且不依赖于数据的大小,但是数据将与原始对象共享,而深层副本不相互依赖(原始副本和深层副本),但是代价是较慢,价格更高的副本。

So it become an obvious question that when we should go for deep copy and when for shallow copy, let's have a try:

因此,当我们应该进行深层复制和何时进行浅层复制时,我们可以尝试一下:

-If Object has been consist of more reference type members. In that case shallow copy will be not effective and deep copy will be a good option.

-如果Object已包含更多引用类型成员。 在这种情况下,浅表复制将无效,而深表复制将是一个不错的选择。

-If you have object which consist of simple data type members(value type), In that case shallow copy will be a good option as it is fast and less expensive in terms of memory.

-如果您的对象包含简单的数据类型成员(值类型),那么在这种情况下,浅表复制将是一个不错的选择,因为它在内存方面既快速又便宜。

----------------------------------------------------------------------------------------------------------------------------------------------------------------

-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----

In this article I just tried to explain the shallow copy and deep copy basic concepts, difference between them and their implementation, For the implementation part I'll  say there are many and many examples and different ways are possible to explain these concepts but I tried the simplest one [ as time is a big factor :) ].

在本文中,我只是试图解释浅层复制和深层复制的基本概念,它们与它们的实现之间的区别,对于实现部分,我将说有很多例子,并且有不同的方式可以解释这些概念,但是我尝试了最简单的一个[因为时间是一个很大的因素:)]。

Even while creating deep copy you can go for serialization with your own formatters and for shallow copy Memberwiseclone.

即使创建深拷贝,您也可以使用自己的格式化程序进行序列化,并使用浅拷贝Memberwiseclone进行序列化。

For deep copy you should be very careful towards your requirement, I mean you need to be sure of what you want, i.e you want a deep copy for same object type or more than one object type.

对于深层复制,您应该非常小心地满足自己的要求,这意味着您需要确定所需的内容,即,您想要针对同一对象类型或多个对象类型进行深层复制。

Try to explore this concepts by using more real time scenario's, I'm sure It will become much more easier to understand.

尝试通过使用更实时的场景来探索这个概念,我相信它将变得更加容易理解。

I hope this article will be of some use.  

我希望本文会有所帮助。

翻译自: https://www.experts-exchange.com/articles/3019/Deep-copy-Vs-Shallow-Copy-in-NET.html

.net 深层拷贝 浅层

你可能感兴趣的:(c#,java,大数据,python,编程语言)