Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Confused about shallow and deep copy in C#

Confused about shallow and deep copy in C#

Scheduled Pinned Locked Moved C#
csharpperformancetutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Johny Ng
    wrote on last edited by
    #1

    Hi folks, I am kind of confused about C# def. about shallow copy. As far as I know, shallow copy points to the same memory location as the orginal, so if we make some changes to the copy, the orginal will be changed. I did a test by doing the followings: ArrayList a=new ArrayList(); a.Add(1); a.Add(2); ArrayList b=(ArrayList)a.Clone() /*C# says it will create a shallow copy here */ b.Add(3); Now a and b are difference, I am confused here. What am I misunderstading? And how to create a true deep and shallow copy in C#? Thanks.

    J A 2 Replies Last reply
    0
    • J Johny Ng

      Hi folks, I am kind of confused about C# def. about shallow copy. As far as I know, shallow copy points to the same memory location as the orginal, so if we make some changes to the copy, the orginal will be changed. I did a test by doing the followings: ArrayList a=new ArrayList(); a.Add(1); a.Add(2); ArrayList b=(ArrayList)a.Clone() /*C# says it will create a shallow copy here */ b.Add(3); Now a and b are difference, I am confused here. What am I misunderstading? And how to create a true deep and shallow copy in C#? Thanks.

      J Offline
      J Offline
      Joshua Quick
      wrote on last edited by
      #2

      First, you need to understand the difference between value types and reference types. All structures and primitives (such as int, long, float) are value types. Value types exist in the stack and are always copied. For example: int a = 1; // Set a to 1. int b = a; // Copy a's value to b. All classes are reference types and their objects exist in the heap. Variables used to access class objects really only store addresses (so to speak) to their objects. Keyword "new" creates an object and returns a reference to that object. For example: ArrayList a = new ArrayList(); // 'a' refers to new ArrayList object. ArrayList b = a; // b copies a's reference, but refers to same object. Now, a shallow copy means that only an object's member variables are copied. This means the value types and references are copied, but the referenced objects themselves are not copied/cloned. A deep copy is when you clone the referenced objects too. You can do this by overriding your class' Clone() method and explicitly clone your reference member variables. In the case of your ArrayList example, it's storing integers which are value types. So, changing the cloned ArrayList's items will not effect the original ArrayList. However, if the original ArrayList stored reference types instead, then that would be another story. Phew! That was a bit long winded, but I hope that made sense.

      1 Reply Last reply
      0
      • J Johny Ng

        Hi folks, I am kind of confused about C# def. about shallow copy. As far as I know, shallow copy points to the same memory location as the orginal, so if we make some changes to the copy, the orginal will be changed. I did a test by doing the followings: ArrayList a=new ArrayList(); a.Add(1); a.Add(2); ArrayList b=(ArrayList)a.Clone() /*C# says it will create a shallow copy here */ b.Add(3); Now a and b are difference, I am confused here. What am I misunderstading? And how to create a true deep and shallow copy in C#? Thanks.

        A Offline
        A Offline
        ABC_Developer
        wrote on last edited by
        #3

        the example you shown is shallow copy. To achieved deep copy you need to create an object for arraylist and not a reference to it ArrayList b = new ArrayList(). int i = 0 while (i < b.count) { a[i] = b[i]; i++; } shallow copy mean two pointers pointing at the same piece of memory.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups