c# - Strange behavior of Array.Clone() -


i have static array below

static byte[] myarray = {0x01,0x02}; 

i read array.clone shallow copy.then execute below codes.

byte[] myarray2 = myarray.clone() byte[]; myarray2[0] = 0x05; 

but each myarray2[0] , myarray[0] contains different values.so think array.clone() performing deep copy .can explain why?

byte primitive type, not reference. there no difference between shallow , deep copy in case.

try using array of mutable object type, , notice difference.

update

but byte[] reference type right?

an array of primitive type deep down represented contiguous block of memory, physically (by value) containing elements. whereas array of object type contiguous block of memory, containing references actual elements (or nulls). when copy array, in first case new array containing copies of elements of original. after that, modifying element in either of arrays won't change contents of other array. whereas in second case new array containing copies of references in original, still pointing same objects referred original array elements. if modify of elements in either of array, change visible in other array too.


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -