I want to create deep copy method and I found 3 ways to execute it
1-deep copy with pass each property 1 by 1
2-using reflection
3-using serialization
please which of them is the best at performance wise
See Question&Answers more detail:osI want to create deep copy method and I found 3 ways to execute it
1-deep copy with pass each property 1 by 1
2-using reflection
3-using serialization
please which of them is the best at performance wise
See Question&Answers more detail:osThe first option, manually deep copying your values, will be the most performant by far.
Reflection will introduce quite a bit of overhead, as it is (relatively) slow to access data.
Serialization is adding a huge cost, as it serializes the data into a temporary structure, then reverses the process to set. This is again, very slow.
The only advantage to option 2 or 3 is that its potentially easier to implement, and reusable across multiple types. The first option has to be hand-written per type, but is much faster (and more efficient in memory usage than option 3, as well).