Share c# clone object

conghoanpigeon

New member
## C# Clone Object

Nhân bản một đối tượng trong C# là quá trình tạo một đối tượng mới có cùng trạng thái với đối tượng gốc.Điều này có thể hữu ích để tạo một bản sao của một đối tượng mà sau đó bạn có thể sửa đổi mà không ảnh hưởng đến đối tượng gốc.

Có hai cách để sao chép một đối tượng trong C#:

*** Nhân bản nông: ** Điều này tạo ra một đối tượng mới có cùng giá trị với đối tượng gốc, nhưng không tạo các bản sao mới của các tài liệu tham khảo của đối tượng.Điều này có nghĩa là nếu đối tượng gốc có bất kỳ tham chiếu nào đến các đối tượng khác, các tài liệu tham khảo đó sẽ được chia sẻ giữa đối tượng gốc và đối tượng nhân bản.
*** Nhân bản sâu: ** Điều này tạo ra một đối tượng mới có cùng giá trị với đối tượng gốc và cũng tạo ra các bản sao mới của các tham chiếu của đối tượng.Điều này có nghĩa là nếu đối tượng gốc có bất kỳ tài liệu tham khảo nào đến các đối tượng khác, các tài liệu tham khảo đó sẽ là duy nhất cho đối tượng nhân bản.

Mã sau đây cho thấy cách sao chép nông một đối tượng trong C#:

`` `C#
// Tạo một đối tượng cho bản sao.
var gốcBject = new myObject ();

// Tạo một đối tượng mới là bản sao nông của đối tượng gốc.
var clonedObject = (myObject) gốcBject.clone ();

// Xác minh rằng đối tượng nhân bản có các giá trị giống như đối tượng gốc.
Assert.areequal (gốc
Assert.areequal (gốc
`` `

Mã sau đây cho thấy cách sao chép sâu một đối tượng trong C#:

`` `C#
// Tạo một đối tượng cho bản sao.
var gốcBject = new myObject ();

// Tạo một đối tượng mới là bản sao sâu của đối tượng gốc.
var clonedObject = new myObject (bản gốc);

// Xác minh rằng đối tượng nhân bản có các giá trị giống như đối tượng gốc.
Assert.areequal (gốc
Assert.areequal (gốc

// Xác minh rằng đối tượng nhân bản có các tài liệu tham khảo riêng cho các đối tượng khác.
Assert.arenotsame (gốc
Assert.arenotsame (gốc
`` `

### hashtags

* #C#
* #object nhân bản
* #shallow Nhân bản
* #deep nhân bản
* #object tham khảo
=======================================
## C# Clone Object

Cloning an object in C# is the process of creating a new object that has the same state as the original object. This can be useful for creating a copy of an object that you can then modify without affecting the original object.

There are two ways to clone an object in C#:

* **Shallow cloning:** This creates a new object that has the same values as the original object, but does not create new copies of the object's references. This means that if the original object has any references to other objects, those references will be shared between the original object and the cloned object.
* **Deep cloning:** This creates a new object that has the same values as the original object, and also creates new copies of the object's references. This means that if the original object has any references to other objects, those references will be unique to the cloned object.

The following code shows how to shallow clone an object in C#:

```c#
// Create an object to clone.
var originalObject = new MyObject();

// Create a new object that is a shallow clone of the original object.
var clonedObject = (MyObject)originalObject.Clone();

// Verify that the cloned object has the same values as the original object.
Assert.AreEqual(originalObject.Property1, clonedObject.Property1);
Assert.AreEqual(originalObject.Property2, clonedObject.Property2);
```

The following code shows how to deep clone an object in C#:

```c#
// Create an object to clone.
var originalObject = new MyObject();

// Create a new object that is a deep clone of the original object.
var clonedObject = new MyObject(originalObject);

// Verify that the cloned object has the same values as the original object.
Assert.AreEqual(originalObject.Property1, clonedObject.Property1);
Assert.AreEqual(originalObject.Property2, clonedObject.Property2);

// Verify that the cloned object has its own unique references to other objects.
Assert.AreNotSame(originalObject.Reference1, clonedObject.Reference1);
Assert.AreNotSame(originalObject.Reference2, clonedObject.Reference2);
```

### Hashtags

* #C#
* #object Cloning
* #shallow Cloning
* #deep Cloning
* #object References
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top