复制具有引用类型作为成员的值类型

时间:2016-03-22 03:42:56

标签: c# value-type reference-type

如果我复制一个具有引用类型成员的值类型(在我的例子中为字符串),CLR会执行浅表副本(book)。所以我写了一个小程序只是为了实验,但无法得到预期的结果。我相信我在这里错过了一些细节。

public class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine("First Example");
        //first example
        Point p1 = new Point();
        p1.Name = "firstPoint";
        p1.X = 10;
        p1.Y = 20;

        Point p2 = p1;

        p2.Name = "secondPoint";
        p2.X = 40;
        p2.Y = 50;

        p1.Print(); //Prints - Name: firstPoint, X:10, Y:20
                    //Expected - Name: secondPoint, X:10, Y:20
        p2.Print(); //Prints - Name: secondPoint, X:40, Y:50

        Console.ReadLine();
    }
}

public struct Point
{
    public string Name;
    public int X;
    public int Y;

    public void Print()
    {
        Console.WriteLine("Name: {0}, X: {1}, Y: {2}", this.Name.ToString(), this.X, this.Y);
    }
}

1 个答案:

答案 0 :(得分:1)

您已经更改了整个引用类型,因此它的行为类似于您可以尝试更改该引用类型的成员以验证和播放它。

请参阅以下代码供您参考:

public class Program
{
    public static void Main()
    {
        Console.WriteLine("First Example");
        //first example
        Point p1 = new Point();
        p1.store = new SomeClass();
        p1.store.Name = "Jenish";
        p1.Name = "firstPoint";
        p1.X = 10;
        p1.Y = 20;

        Point p2 = p1;

        p2.Name = "secondPoint";
        p2.store.Name = "Jenish2";
        p2.X = 40;
        p2.Y = 50;

        p1.Print(); //Prints - Name: firstPoint, X:10, Y:20
        p2.Print(); //Prints - Name: secondPoint, X:40, Y:50

        Console.ReadLine();
    }
}

public struct Point
{

    public string Name;
    public int X;
    public int Y;
    public SomeClass store;

    public void Print()
    {
        Console.WriteLine("Name: {0}, X: {1}, Y: {2}, Name: {3}", this.Name.ToString(), this.X, this.Y, this.store.Name);
    }
}

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

<强>输出

  

第一个例子

     

姓名:firstPoint,X:10,Y:20,姓名:Jenish2

     

姓名:secondPoint,X:40,Y:50,姓名:Jenish2

以下是fiddle

下面的示例演示了更改整个引用类型时会发生什么情况。

public class Program
{
    public static void Main()
    {
        Console.WriteLine("First Example");
        //first example
        Point p1 = new Point();
        p1.store = new SomeClass();
        p1.store.Name = "Jenish";
        p1.Name = "firstPoint";
        p1.X = 10;
        p1.Y = 20;

        Point p2 = p1;

        p2.Name = "secondPoint";
        p2.store = new SomeClass();
        p2.store.Name = "Jenish2";
        p2.X = 40;
        p2.Y = 50;

        p1.Print(); //Prints - Name: firstPoint, X:10, Y:20
        p2.Print(); //Prints - Name: secondPoint, X:40, Y:50

        Console.ReadLine();
    }
}

public struct Point
{

    public string Name;
    public int X;
    public int Y;
    public SomeClass store;

    public void Print()
    {
        Console.WriteLine("Name: {0}, X: {1}, Y: {2}, Name: {3}", this.Name.ToString(), this.X, this.Y, this.store.Name);
    }
}

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

注意:对象中的名称不同。

第二个例子是fiddle