See also:
Assume we have a generic List of objects of type CWebPage. The CWebPage class contains a System.Uri object (a reference type) and an integer Count (a value type).
A shallow copy is useful if we want to sort the list. In that case we specifically don't want a deep copy. We don't want to duplicate the list items, only the list object and the reference variables that point to them.
Implementing ICloneable on CWebPage.
See also:
Article continues below Ad.
We first need to implement the interface ISerializable on our CWebPage class.
A small utility function encapsulates the calls to Serialize and Deserialize:
We can now create a deep copy of the list using ISerializable by calling CopyUsingSerializer.
The serialized string (JSON text) looks like this:
[ {"Uri":"http://www.google.com","Count":10}, {"Uri":"http://www.yahoo.com","Count":20}, {"Uri":"http://www.bing.com","Count":30} ]