ICloneable Interface


支援複製 (Clone),使用與現有執行個體 (Instance) 相同的值建立新的執行個體。Supports cloning, which creates a new instance of a class with the same value as an existing instance.

public interface class ICloneable
public interface ICloneable
public interface ICloneable
type ICloneable = interface
Public Interface ICloneable


ICloneable 介面可讓您提供自訂的執行,以建立現有物件的複本。The ICloneable interface enables you to provide a customized implementation that creates a copy of an existing object. ICloneable 介面包含一個成員,也就是 Clone 方法,目的是要提供超過 Object.MemberwiseClone所提供的複製支援。The ICloneable interface contains one member, the Clone method, which is intended to provide cloning support beyond that supplied by Object.MemberwiseClone. 如需複製、深度與淺層複本和範例的詳細資訊,請參閱 Object.MemberwiseClone 方法。For more information about cloning, deep versus shallow copies, and examples, see the Object.MemberwiseClone method.

Notes to Implementers

ICloneable 介面只需要您的 Clone() 方法的執行,才會傳回目前物件實例的複本。The ICloneable interface simply requires that your implementation of the Clone() method return a copy of the current object instance. 它不會指定複製作業會執行深層複製、淺層複製或介於其間的某個專案。It does not specify whether the cloning operation performs a deep copy, a shallow copy, or something in between. 也不需要將原始實例的所有屬性值複製到新的實例。Nor does it require all property values of the original instance to be copied to the new instance. 例如,Clone() 方法會執行所有屬性的淺層複本,但不包括 IsReadOnly 屬性;它一律會將此屬性值設定為複製之物件中的 falseFor example, the Clone() method performs a shallow copy of all properties except the IsReadOnly property; it always sets this property value to false in the cloned object. 因為 Clone() 的呼叫端無法相依于執行可預測複製作業的方法,所以我們建議您不要在公用 Api 中執行 ICloneableBecause callers of Clone() cannot depend on the method performing a predictable cloning operation, we recommend that ICloneable not be implemented in public APIs.



建立新的物件,這個物件是目前執行個體的複本。Creates a new object that is a copy of the current instance.

Applies to

See also