开发者

clone object in c#

开发者 https://www.devze.com 2023-02-10 03:19 出处:网络
What is clone object and types?can give simple example ?开发者_如何学运维and is possible call clone object to List ?Look here: Deep cloning objectsA clone is a copy (i.e. a new instance). Several BCL

What is clone object and types?can give simple example ?开发者_如何学运维and is possible call clone object to List ?


Look here: Deep cloning objects


A clone is a copy (i.e. a new instance). Several BCL classes implement IClonable which returns an object which should be a new instance with the values of the original.

A good sample is at:

http://msdn.microsoft.com/en-us/library/system.icloneable.aspx

I don't think List<T> supports it, but you could add it, as long as the T are IClonable.


Cloning means creating another instance of your Reference type (Anything that is not a constant [integers, chars...] or a structure) so you can modify one of them without affecting the other, as just using the Equals operator or passing one of such value types would create a pseudo-pointer.

To clone your classes, just make them implement ICloneable [http://msdn.microsoft.com/en-us/library/system.icloneable.aspx] and call the Clone() Method, casting the return type to the object type desired.

Good Luck :)

0

精彩评论

暂无评论...
验证码 换一张
取 消