This can be used to clone an object however this is a C++
term, this allows you to specify an object to create the data from, it would not be very idiomatic for a C#
developer so I don’t feel its the best approach for me as a .Net Developer.
Would require each custom type to have a copy constructor to allow traversing of the tree so the copy is done recursively. If you want to clone rather look at the suggestions under the creational Prototype Pattern.
Example
Example classes Person
and Address
which have copy constructors:
1 | public class Person |
This will work as strings are immutable.
1 | var carl = new Person("Carl", new Address("Sale Street", 66)); |
Output would be:
1 | FirstName: Carl, Address: StreetName: Sale Street, HouseNumber: 66 |
References
- [PrototypePattern/CopyConstructorDemo.cs](https://github.com/carlpaton/Boilerplate/blob/master/.Net Core Console Application/PatternsAndPrinciples/PrototypePattern/CopyConstructorDemo.cs)