clone object in c#
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 :)
精彩评论