Hello,
I Want to pass a class to a function but and, in the function, do a variety of tests to the class that modify it.
How do I create a copy of the class in order to modify the copy instead of the original, since reference types are always passed by reference?
Thank you
AlanPosted Aug 29, 2007, 1:03 PM
Basically, your class needs to provide a method to clone the object and then pass the clone to the function.
There are two types of cloning 'shallow' and 'deep'. The difference is that a deep clone doesn't just copy the references held by reference-type fields but the actual objects to which those references refer.
Shallow cloning is sufficient for many purposes and can be implemented using the protected MemberwiseClone method which all classes inherit from System.Object.
For more details can I refer you to these Code Project articles:
http://www.codeproject.com/dotnet/Clone.asp
http://www.codeproject.com/csharp/cloneimpl_class.asp