✅ How to force C# to copy data?
I am trying to debug code and I have a list of
rooms
and another list of backup_rooms
. They both get initialized at the same time, but when I need use backup room I find that is has changed (It is never used in code). My guess is, that a reference is created to an object rather than a copy of it. So how to force C# to copy?7 Replies
do you have an example of your code?
in general yes, reference types (classes) work similarly to passing pointers around in C/C++
so if you're creating a room and adding it to both lists, they're referencing the same room object
is there a function for creating a deep copy of an object or do I have to make a new object myself?
you have to make a copy yourself, there's no built in function for that because exactly what a "deep copy" is depends on the object
this is the part where the reference is created.
thanks
I would use copy constructors to aid you in your quest