简体   繁体   English

Array.Clone()的奇怪行为

[英]Strange behavior of Array.Clone()

I have a static array like below 我有一个像下面的静态数组

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

I read that Array.clone does shallow copy .Then i execute the below codes. 我读到Array.clone进行浅拷贝 。然后执行以下代码。

byte[] myArray2 = myArray.Clone() as byte[];
myArray2[0] = 0x05;

But now Each myArray2[0] and myArray[0] contains different values.So I think Array.Clone() is performing a deep copy .Can you explain why? 但是现在每个myArray2 [0]和myArray [0]包含不同的值。所以我认为Array.Clone()正在执行深层复制。您能解释为什么吗?

byte is a primitive type, not a reference. byte是原始类型,而不是引用。 Thus there is no difference between shallow and deep copy in this case. 因此,在这种情况下,浅拷贝和深拷贝之间没有区别。

Try using an array of a mutable object type, and you will notice the difference. 尝试使用可变对象类型的数组,您会注意到其中的区别。

Update 更新资料

but byte[] is a reference type right? 但是byte []是引用类型对吗?

An array of a primitive type is deep down most probably represented by a contiguous block of memory, physically ( by value ) containing the elements. 基本类型的数组很可能是由内存中连续的块表示的,该块物理上( 按值 )包含元素。 Whereas an array of an object type is a contiguous block of memory, containing only references to the actual elements (or null s). 而对象类型的数组是一个连续的内存块,仅包含对实际元素的引用 (或null )。 Thus when you copy the array, in the first case you get a new array containing copies of the elements of the original. 因此,在复制数组时,在第一种情况下,您将获得一个包含原始元素副本的新数组。 So after that, modifying an element in either of the arrays won't change the contents of the other array. 因此,在那之后,修改两个数组中的任何一个元素都不会更改另一个数组的内容。 Whereas in the second case you get a new array containing copies of the references in the original, still pointing to the same objects referred to by the original array elements . 在第二种情况下,您将获得一个新数组,其中包含原始引用的副本,但仍指向原始数组元素引用的相同对象 So if you modify any of the elements in either of the array, the change will be visible in the other array too. 因此,如果您修改任一数组中的任何元素,那么更改也将在另一个数组中可见。

A shallow copy means it copies the immediate values. 浅表副本表示它复制立即值。 If those values are pointers (reference types like objects in C#), then the pointers are copied over, and the values they point to (the state of the object) is not copied. 如果这些值是指针(引用类型如C#中的对象),则将指针复制过来,并且它们所指向的值(对象的状态)不会被复制。

If the immediate values are primitives (ie: the value itself resides in the array, it's not referenced ) then those values are copied. 如果立即值是基元(即:值本身位于数组中,则未引用 ),则将这些值复制。 Hence the behavior is exactly as expected. 因此,行为完全符合预期。

If you want to see the difference in your case, define an array of Object s. 如果要查看情况的不同,请定义一个Object数组。

Try this: 尝试这个:

public class Person
{
    public string Name { get; set; }
}

Person person = new Person { Name = "Vaysage" };

Person[] persons1 = new Person[] { person  };

Person[] persons2 = (Person[])persons1.Clone();

persons2[0].Name = ".NET Junkie";

Assert.AreEqual(persons1[0].Name, ".NET Junkie");

The arrays are shallowly copied. 数组被浅复制。 This means that just references to the Person objects are copied and not the whole objects. 这意味着只复制对Person对象的引用,而不复制整个对象。 Both arrays reference the same person object and thus the name of the person in the first array is changed as well. 两个数组都引用同一个人对象,因此第一个数组中的个人名称也将更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM