✅ Deep copy struct

There are a couple arrays sitting in Board and they get mutated by tempBoard.MakeMove()
No description
63 Replies
qqdev
qqdev3w ago
Hi! What's the question here?
ElectricTortoise
ElectricTortoiseOP3w ago
how do i stop tempBoard from modifying board's data basically how do i deep copy the struct
Jimmacle
Jimmacle3w ago
there is no simple solution, you'll have to implement a deep copy for the type yourself that makes copies of any contained reference types structs are passed/assigned by value, but arrays are reference types so only the references are copied and both still point to the same array
ElectricTortoise
ElectricTortoiseOP3w ago
yea... so thats what im asking lol
Jimmacle
Jimmacle3w ago
for arrays specifically you can just sourceArray.ToArray()
ElectricTortoise
ElectricTortoiseOP3w ago
i found this on documentation, i probably cannot do the first one
No description
ElectricTortoise
ElectricTortoiseOP3w ago
this is the 2nd method? wait hang on does this only apply to classes
Jimmacle
Jimmacle3w ago
in your case the easiest way is just to add a Clone method to your struct that returns a new struct with updated references or whatever you want to call it, it's not a standard part of the language so it's up to you
Sehra
Sehra3w ago
and if you want to copy an array, doing arr.ToArray() is a quick way
ElectricTortoise
ElectricTortoiseOP3w ago
oh ok this is for the array only? this would handle the entire struct?
Jimmacle
Jimmacle3w ago
it handles what you handle inside the method in your case you need to make copies of the arrays yourself and assign them to the new struct
ElectricTortoise
ElectricTortoiseOP3w ago
oh ok
qqdev
qqdev3w ago
I usually create an IDeepCloneable interface to make it clear that we are dealing with a deep copy here
ElectricTortoise
ElectricTortoiseOP3w ago
and put it under the clone() method? ooo wait can i do that for a struct
Jimmacle
Jimmacle3w ago
e.g.
var newStruct = this;
newStruct.Array = this.Array.ToArray();
return newStruct;
var newStruct = this;
newStruct.Array = this.Array.ToArray();
return newStruct;
qqdev
qqdev3w ago
But you can start off with ICloneable
ElectricTortoise
ElectricTortoiseOP3w ago
structs dont have inheritance right ooo nice also what is the difference between array.CopyTo() and array.ToArray()
Sehra
Sehra3w ago
not inheritance, but you can implement interfaces
Jimmacle
Jimmacle3w ago
ToArray creates a new array for you, CopyTo copies the elements into an existing array also, if your array elements are mutable reference types you'll need to copy all of them as well
ElectricTortoise
ElectricTortoiseOP3w ago
sheesh wait does this include 2D arrays
Jimmacle
Jimmacle3w ago
no
ElectricTortoise
ElectricTortoiseOP3w ago
oh so only like array of classes
Jimmacle
Jimmacle3w ago
right, the element type determines whether you have to implement a deep copy for each element or not
ElectricTortoise
ElectricTortoiseOP3w ago
got it
Jimmacle
Jimmacle3w ago
if you're familiar with C or C++ at all, reference types are similar to newing up some memory and assigning the pointer to a variable in that if you copy a struct that has a reference type in it, you're only copying the reference/pointer and not the actual data being referenced
ElectricTortoise
ElectricTortoiseOP3w ago
i plan on learning that next
Sehra
Sehra3w ago
T Clone<T>(T value) => JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(value)); not very efficient but kinda works most of the time
Jimmacle
Jimmacle3w ago
assuming the type is serializable to json yeah, it's technically a solution
Sehra
Sehra3w ago
but it's generally better to implement ICloneable when needed
ElectricTortoise
ElectricTortoiseOP3w ago
mmm ok
Leo
Leo3w ago
Can I help you? what is the problem?
ElectricTortoise
ElectricTortoiseOP3w ago
im trying to deep copy a struct ok i just noticed that i have a class inside my struct, and this class doesnt have any reference types so in this case do i do
var newStruct = this;
newStruct.Array = this.Array.ToArray();
newStruct.Class = this.Class.MemberwiseClone()
return newStruct;
var newStruct = this;
newStruct.Array = this.Array.ToArray();
newStruct.Class = this.Class.MemberwiseClone()
return newStruct;
Leo
Leo3w ago
maybe the correct version is: public struct MyStruct { public int[] Array; public MyClass Class; public MyStruct DeepCopy() { var newStruct = this; // Create a copy of the struct newStruct.Array = (int[])this.Array.Clone(); // Deep copy of the array newStruct.Class = (MyClass)this.Class.MemberwiseClone(); // Deep copy of the class return newStruct; } }
ElectricTortoise
ElectricTortoiseOP3w ago
ok yea i realise my syntax is incorrect
Leo
Leo3w ago
Do you understand?
Jimmacle
Jimmacle3w ago
MemberwiseClone is not a deep copy it is explicitly a shallow copy there is also no Clone method on arrays, the closest is ToArray and it is also a shallow copy (it won't deep copy elements)
ElectricTortoise
ElectricTortoiseOP3w ago
. shallow copy and deep copy are the same when there are no reference types right
Jimmacle
Jimmacle3w ago
essentially
Leo
Leo3w ago
okay, you are right
ElectricTortoise
ElectricTortoiseOP3w ago
do i need to perform MemberwiseClone() within the mini-class or can i call it from the struct
ElectricTortoise
ElectricTortoiseOP3w ago
it doesnt wanna behave lol
No description
Jimmacle
Jimmacle3w ago
if the class only contains value types then MemberwiseClone should be fine
ElectricTortoise
ElectricTortoiseOP3w ago
yes it does
Leo
Leo3w ago
to achieve a true deep copy, public struct MyStruct { public int[] Array; public MyClass Class; public MyStruct DeepCopy() { var newStruct = new MyStruct(); newStruct.Array = new int[this.Array.Length]; Array.Copy(this.Array, newStruct.Array, this.Array.Length); // Copy elements // Assuming MyClass has a custom method for deep copying newStruct.Class = this.Class.DeepCopy(); // Implement DeepCopy in MyClass return newStruct; } } public class MyClass { public int Property; // Example property public MyClass DeepCopy() { return new MyClass { Property = this.Property }; // Adjust for your properties } }
ElectricTortoise
ElectricTortoiseOP3w ago
i do think i am implementing it the same way
No description
Jimmacle
Jimmacle3w ago
you need to call MemberwiseClone on the object you want to clone
ElectricTortoise
ElectricTortoiseOP3w ago
you can use 3` like quotes to make the code look cleaner
Leo
Leo3w ago
okay
Jimmacle
Jimmacle3w ago
newStruct.Array = new int[this.Array.Length];
Array.Copy(this.Array, newStruct.Array, this.Array.Length); // Copy elements
newStruct.Array = new int[this.Array.Length];
Array.Copy(this.Array, newStruct.Array, this.Array.Length); // Copy elements
this is unnecessary, just call ToArray()
ElectricTortoise
ElectricTortoiseOP3w ago
oh so from within boardState itself i need the clone method?
No description
Jimmacle
Jimmacle3w ago
that should be this.boardState.MemberwiseClone() and probably cast it back to whatever type boardState is
Sehra
Sehra3w ago
MemberwiseClone is protected
ElectricTortoise
ElectricTortoiseOP3w ago
yea that didnt work
Jimmacle
Jimmacle3w ago
in that case you'll have to do what you showed in your image but inside the class you want to copy, not the struct
ElectricTortoise
ElectricTortoiseOP3w ago
got it
Sehra
Sehra3w ago
what type is boardState?
ElectricTortoise
ElectricTortoiseOP3w ago
it's just a class
Jimmacle
Jimmacle3w ago
a class with only value typed members, based on previous conversation
Leo
Leo3w ago
Are you not clear?
ElectricTortoise
ElectricTortoiseOP3w ago
okok thank you guys so much my code is working now yes
Leo
Leo3w ago
great. I am glad
ElectricTortoise
ElectricTortoiseOP3w ago
.close how do i close this !close
Accord
Accord3w ago
Closed!

Did you find this page helpful?