ASP.NET----深浅コピー問題

9029 ワード

                   ,       ,                     ,              ,        ,                    。

 
   

   
     
using System;
using System.Collections.Generic;
using System.Text;

namespace NET.NST.Third.DeepCopy
{
public class Program
{
static void Main( string [] args)
{
DeepCopy dc
= new DeepCopy();
dc._i
= 10 ;
dc._a
= new A();
DeepCopy deepclone
= (DeepCopy)dc.Clone();
DeepCopy shadowclone
= (DeepCopy)dc.MemberwiseClone();

//
//
deepclone._a._s = " A " ;
Console.WriteLine(dc);
Console.WriteLine(deepclone);
Console.WriteLine(
" \r
" );

//
//
shadowclone._a._s = " A " ;
Console.WriteLine(dc);
Console.WriteLine(shadowclone);
Console.Read();
}
}

public class DeepCopy : ICloneable
{
public int _i = 0 ;
public A _a = new A();

public object Clone()
{
//
DeepCopy newdc = new DeepCopy();
newdc._a
= new A();
newdc._a._s
= _a._s;
newdc._i
= _i;
return newdc;
}

//
public new object MemberwiseClone()
{
return base .MemberwiseClone();
}
public override string ToString()
{
return " I : " + _i.ToString() + " ,A : " + _a._s;
}
}
public class A
{
public String _s = " A " ;
}
}