我贊同@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」的拷貝。 –