我赞同@docesam的回答和@Will Yu的部分答案。
这既不是浅也不是深拷贝,这是参考拷贝。 - docesam
OB2 = OB1;此代码创建两个引用同一个对象的对象引用。因此,通过ob1所做的对对象的任何更改都会反映在ob2的后续使用中。 --Will宇
根据MSDN (see Remarks):
阵列只复制数组的元素,无论是引用类型还是值类型的浅拷贝,但它不复制引用引用的对象。新数组中的引用指向与原始数组中的引用指向的相同对象。
在这里,我们有两点需要注意:
- 浅表副本复印件元素。
- 浅拷贝保留了元素的原始引用。
接下来,让我分别解释这两个。
首先,我们创建了一个Person
类与Name
属性:
class Person
{
public string Name {get; set;}
}
然后在Main()
方法,我们创建一个Person
阵列。
// Create 2 Persons.
var person1 = new Person(){ Name = "Jack" };
var person2 = new Person(){ Name = "Amy" };
// Create a Person array.
var arrPerson = new Person[] { person1, person2 };
1.一种浅拷贝复制的元件。
如果我们更换在浅复制的第一个元素,原始数组不应受到影响:
// Create a shallow copy.
var arrPersonClone = (Person[]) arrPerson.Clone();
// Replace an element in the shallow copy.
arrPersonClone[0] = new Person(){Name = "Peter"};
// Display the contents of all arrays.
Console.WriteLine("After replacing the first element in the Shallow Copy");
Console.WriteLine($"The Original Array: {arrPerson[0].Name}, {arrPerson[1].Name}");
Console.WriteLine($"The Shallow Copy: {arrPersonClone[0].Name}, {arrPersonClone[1].Name}");
结果:
The Original Array: Jack, Amy
The Shallow Copy: Peter, Amy
2.一种浅副本保留了元素的原始参考。
如果我们更改浅复制的元件的属性,原始数组会受到影响,因为以不被复制该元素使参考的对象。
// Create a new shallow copy.
arrPersonClone = (Person[]) arrPerson.Clone();
// Change the name of the first person in the shallow copy.
arrPersonClone[0].Name = "Peter";
// Display the contents of all arrays.
Console.WriteLine("After changing the Name property of the first element in the Shallow Copy");
Console.WriteLine($"The Original Array: {arrPerson[0].Name}, {arrPerson[1].Name}");
Console.WriteLine($"The Shallow Copy: {arrPersonClone[0].Name}, {arrPersonClone[1].Name}");
结果:
The Original Array: Peter, Amy
The Shallow Copy: Peter, Amy
那么,如何简单的等号,=
,循规蹈矩?
它作出参考副本。对元素或引用对象的任何更改都会反映在原始数组和“复制”数组中。
// Create a reference copy.
var arrPersonR = arrPerson;
// Change the name of the first person.
arrPersonR[0].Name = "NameChanged";
// Replace the second person.
arrPersonR[1] = new Person(){ Name = "PersonChanged" };
// Display the contents of all arrays.
Console.WriteLine("After changing the reference copy:");
Console.WriteLine($"The Original Array: {arrPerson[0].Name}, {arrPerson[1].Name}");
Console.WriteLine($"The Reference Copy: {arrPersonR[0].Name}, {arrPersonR[1].Name}");
结果:
The Original Array: NameChanged, PersonChanged
The Reference Copy: NameChanged, PersonChanged
总之,ob2 = ob1
不浅拷贝,但参考副本。
void Main()
{
// Create 2 Persons.
var person1 = new Person(){ Name = "Jack" };
var person2 = new Person(){ Name = "Amy" };
// Create a Person array.
var arrPerson = new Person[] { person1, person2 };
// ----------- 1. A shallow copy copies elements. -----------
// Create a shallow copy.
var arrPersonClone = (Person[]) arrPerson.Clone();
// Replace an element in the shallow copy.
arrPersonClone[0] = new Person(){Name = "Peter"};
// Display the contents of all arrays.
Console.WriteLine("After replacing the first element in the Shallow Copy:");
Console.WriteLine($"The Original Array: {arrPerson[0].Name}, {arrPerson[1].Name}");
Console.WriteLine($"The Shallow Copy: {arrPersonClone[0].Name}, {arrPersonClone[1].Name}");
Console.WriteLine("\n");
// ----------- 2. A shallow copy retains the original references of the elements. -----------
// Create a new shallow copy.
arrPersonClone = (Person[]) arrPerson.Clone();
// Change the name of the first person in the shallow copy.
arrPersonClone[0].Name = "Peter";
// Display the contents of all arrays.
Console.WriteLine("After changing the Name property of the first element in the Shallow Copy:");
Console.WriteLine($"The Original Array: {arrPerson[0].Name}, {arrPerson[1].Name}");
Console.WriteLine($"The Shallow Copy: {arrPersonClone[0].Name}, {arrPersonClone[1].Name}");
Console.WriteLine("\n");
// ----------- 2. The equal sign. -----------
// Create a reference copy.
var arrPersonR = arrPerson;
// Change the name of the first person.
arrPersonR[0].Name = "NameChanged";
// Replace the second person.
arrPersonR[1] = new Person(){ Name = "PersonChanged" };
// Display the contents of all arrays.
Console.WriteLine("After changing the reference copy:");
Console.WriteLine($"The Original Array: {arrPerson[0].Name}, {arrPerson[1].Name}");
Console.WriteLine($"The Reference Copy: {arrPersonR[0].Name}, {arrPersonR[1].Name}");
}
class Person
{
public string Name {get; set;}
}
这是一个浅拷贝:
的完整代码一起玩。 –
关于对象复制的维基百科页面:http://en.wikipedia.org/wiki/Object_copy – Jerome
@VaughanHilts - 我不会称之为“浅拷贝”,因为上面的代码根本不执行任何“obj1”的拷贝。 –