ICloneable ICloneable ICloneable 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
[System.Runtime.InteropServices.ComVisible(true)]
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.MemberwiseCloneThe 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.

給實施者的注意事項

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屬性,因為它一律將此屬性值設定為false中複製的物件。For 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()無法取決於執行的可預測的方法複製作業,我們建議ICloneable公用 Api 中實作。Because callers of Clone() cannot depend on the method performing a predictable cloning operation, we recommend that ICloneable not be implemented in public APIs.

方法

Clone() Clone() Clone() Clone()

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

適用於

另請參閱