0

I've read that Array.Clone performs shallow copy, however this code suggests that a deep copy of the original array is created i.e. any changes in cloned array are not being reflected in original array

int[] arr = new int[] { 99, 98, 92, 97, 95 };
int[] newArr = (int[])arr.Clone();
//because this is a shallow copy newArr should refer arr
newArr[0] = 100;
//expected result 100
Console.WriteLine(arr[0]);//print 99

Am i missing something obvious here ?

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Ankit
  • 5,733
  • 2
  • 22
  • 23

4 Answers4

2

because this is a shallow copy newArr should refer arr

Nope, the array and its elements are copied. But references to objects in the elements are not copied.

The copy goes down just one level: hence shallow. A deep copy would clone all referenced objects (but this cannot be shown with ints.)

Richard
  • 106,783
  • 21
  • 203
  • 265
1

Try the same code but with a class that has a property that is an integer. Since the array elements are value types the elements of the cloned array are their own "instances".

example (DotNet Fiddle):

using System;
                    
public class Program
{
    class SomeClass {
   
        public Int32 SomeProperty { get; set; }

    }
    
    public static void Main()
    {
        SomeClass[] arr = new [] {
            new SomeClass { SomeProperty = 99 },
            new SomeClass { SomeProperty = 98 },
            new SomeClass { SomeProperty = 92 },
            new SomeClass { SomeProperty = 97 },
            new SomeClass { SomeProperty = 95 }
        };
        
        SomeClass[] newArr = (SomeClass[])arr.Clone();
        
        newArr[0].SomeProperty = 100;
        
        Console.WriteLine(arr[0].SomeProperty);
    }
}
iokevins
  • 1,427
  • 2
  • 19
  • 29
Aaron Roberts
  • 1,342
  • 10
  • 21
0

When copying a collection of immutable structs (primitives such as it's are immutables) there is no difference between deep and shallow copy. They are copied by value - and therefore it is as deep copy performs.

See more for the difference under: What is the difference between a deep copy and a shallow copy?

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • A better description would be "When copying a collection of immutable objects there is no difference between deep and shallow copy.", all primitives are immutable but you can see this same behavior on any set of purely immutable objects. – Scott Chamberlain Sep 17 '17 at 16:24
  • Thinking about it, you need to say "Immutable structs" for the 2nd part of your answer to be true. Immutable classes would still technically behave different, because only one copy of the immutable object will be in memory. – Scott Chamberlain Sep 17 '17 at 16:31
-1

In some advanced Array or List<> is really hard to only use Array.Clone() use instead a plugin like FastDeepCloner which I developed. It will clone the object intrusively.

var newArr= FastDeepCloner.DeepCloner.Clone(arr);
Pang
  • 9,564
  • 146
  • 81
  • 122
Alen.Toma
  • 4,684
  • 2
  • 14
  • 31