偶然看到 Array.Copy 方法的时候,想到,它是否是克隆,又是否是深克隆。

做了一个测试

        public class abc    {        public string hello;    } 

   [TestMethod]
        public void TestArrayCopy()
        {
            abc a = new abc();
            a.hello = "hello ";

            var r = new abc[1];
            Array.Copy(new abc[] { a }, r, 1);

            var b = r[0];
            b.hello = "world";

            Console.WriteLine(a.hello + "," + b.hello);
        }

 

输出: world,world . 看来是它不是克隆。

仔细想想也应该不是克隆。如果是克隆,那克隆就可以用这个方法逆天了。更谈的不上深克隆。

很好奇,看了一下 Reflector . 实现方法着实看不懂。

[MethodImpl(MethodImplOptions.InternalCall), ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail), SecurityCritical]
internal static extern void Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length, bool reliable);

是一个 internal  的,这个见多了,还是一个 extern 的!要命的是属性声明,看了MSDN也不懂。到底在哪实现这玩意。有待研究。

 

PS. Array 继承自  ICloneable , 是可以浅克隆的,但仅克隆Array本身,对里面的内容是不克隆的。

相关文章:

  • 2022-12-23
  • 2021-10-03
  • 2022-12-23
  • 2021-07-25
  • 2021-11-02
  • 2022-12-23
  • 2021-10-02
  • 2021-08-15
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-01
  • 2022-12-23
  • 2022-12-23
  • 2021-08-07
  • 2021-10-25
相关资源
相似解决方案