复制具有引用类型作为成员的值类型
copying a value type that has reference type as a member
如果我复制具有引用类型成员(在我的例子中是字符串)的值类型,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);
}
}
您已经更改了整个引用类型,因此它的行为就像那样,您可以尝试更改该引用类型的成员以验证和解决它。
参考以下代码:
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;}
}
输出
First Example
Name: firstPoint, X: 10, Y: 20, Name: Jenish2
Name: secondPoint, X: 40, Y: 50, Name: 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。
如果我复制具有引用类型成员(在我的例子中是字符串)的值类型,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);
}
}
您已经更改了整个引用类型,因此它的行为就像那样,您可以尝试更改该引用类型的成员以验证和解决它。
参考以下代码:
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;}
}
输出
First Example
Name: firstPoint, X: 10, Y: 20, Name: Jenish2
Name: secondPoint, X: 40, Y: 50, Name: 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。