1. 程式人生 > >C# 淺拷貝與深拷貝區別 解惑篇

C# 淺拷貝與深拷貝區別 解惑篇

staticvoid Main(string[] args)
        {
            Demo();
        }
        
publicstaticvoid Demo()
        {
            DemoClass A 
=new DemoClass();
            DemoClass B 
= (DemoClass)A.Clone();
            B.intValue 
=2;
            Write(
string.Format("        int->[A:{0}] [B:{1}]", A.intValue, B.intValue));
            B.strValue 
="2";
            Write(
string.Format("     string->[A:{0}] [B:{1}]", A.strValue, B.strValue));
            B.pEnum 
= PersonEnum.EnumB;
            Write(
string.Format("       Enum->[A:{0}] [B:{1}]", (int)A.pEnum, (int)B.pEnum));
            B.pStruct.StructValue 
=2;
            Write(
string.Format("
     struct->[A:{0}] [B:{1}]", A.pStruct.StructValue, B.pStruct.StructValue));
            B.pIntArray[
0=2;
            Write(
string.Format("   intArray->[A:{0}] [B:{1}]", A.pIntArray[0], B.pIntArray[0]));
            B.pStringArray[
0="2";
            Write(
string.Format("stringArray->[A:{0}] [B:{1}]
", A.pStringArray[0], B.pStringArray[0]));
            B.pClass.Name 
="2";
            Write(
string.Format("      Class->[A:{0}] [B:{1}]", A.pClass.Name, B.pClass.Name));
            System.Console.Read();
        }
        
staticvoid Write(string msg)
        {
            System.Console.WriteLine(msg);
        }