b*******y 发帖数: 1240 | 1 以下程序
copyto和clone都执行shallow copy,但是为什么程序里面反映不出来,谢谢
int[] array = new int[] { 1, 2, 3, 4, 5, 6 };
//create shallow copy by CopyTo
//You have to instantiate your new array first
int[] array2 = new int[myarray.Length];
//but then you can specify how many members of original array yo
u would like to copy
array.CopyTo(array2, 0);
//create shallow copy by Clone
int[] array1;
//here you don't need to instantiate array,
//but all elements of the original array will be copied
array1 = array.Clone() as int[];
//if not sure that we create a shalow copy lets test it
array[0] = 0;
Console.WriteLine(array[0]);// print 0
Console.WriteLine(array1[0]);//print 1
Console.WriteLine(array2[0]);//print 1
Console.WriteLine(array == array1); // print false | b*******y 发帖数: 1240 | 2 有人知道么
谢谢
yo
【在 b*******y 的大作中提到】 : 以下程序 : copyto和clone都执行shallow copy,但是为什么程序里面反映不出来,谢谢 : int[] array = new int[] { 1, 2, 3, 4, 5, 6 }; : //create shallow copy by CopyTo : //You have to instantiate your new array first : int[] array2 = new int[myarray.Length]; : //but then you can specify how many members of original array yo : u would like to copy : array.CopyTo(array2, 0); : //create shallow copy by Clone
|
|