ICloneable 接口


支持克隆,即用与现有实例相同的值创建类的新实例。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
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.



创建作为当前实例副本的新对象。Creates a new object that is a copy of the current instance.