✅ Deep copy struct
There are a couple arrays sitting in Board and they get mutated by tempBoard.MakeMove()

63 Replies
Hi! What's the question here?
how do i stop tempBoard from modifying board's data
basically how do i deep copy the struct
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
yea... so thats what im asking lol
for arrays specifically you can just
sourceArray.ToArray()
i found this on documentation, i probably cannot do the first one

this is the 2nd method?
wait hang on does this only apply to classes
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 youand if you want to copy an array, doing
arr.ToArray()
is a quick wayoh ok
this is for the array only?
this would handle the entire struct?
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
oh ok
I usually create an
IDeepCloneable
interface to make it clear that we are dealing with a deep copy hereand put it under the clone() method?
ooo
wait can i do that for a struct
e.g.
But you can start off with
ICloneable
structs dont have inheritance right
ooo nice
also what is the difference between array.CopyTo() and array.ToArray()
not inheritance, but you can implement interfaces
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
sheesh
wait does this include 2D arrays
no
oh so only like array of classes
right, the element type determines whether you have to implement a deep copy for each element or not
got it
if you're familiar with C or C++ at all, reference types are similar to
new
ing 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 referencedi plan on learning that next
T Clone<T>(T value) => JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(value));
not very efficient but kinda works most of the timeassuming the type is serializable to json yeah, it's technically a solution
but it's generally better to implement ICloneable when needed
mmm ok
Can I help you?
what is the problem?
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
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;
}
}
ok yea i realise my syntax is incorrect
Do you understand?
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).
shallow copy and deep copy are the same when there are no reference types right
essentially
okay, you are right
do i need to perform MemberwiseClone() within the mini-class or can i call it from the struct
it doesnt wanna behave lol

if the class only contains value types then MemberwiseClone should be fine
yes it does
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
}
}
i do think i am implementing it the same way

you need to call MemberwiseClone on the object you want to clone
you can use 3` like quotes to make the code look cleaner
okay
this is unnecessary, just call ToArray()
oh so from within boardState itself i need the clone method?

that should be
this.boardState.MemberwiseClone()
and probably cast it back to whatever type boardState isMemberwiseClone is protected
yea that didnt work
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
got it
what type is boardState?
it's just a class
a class with only value typed members, based on previous conversation
Are you not clear?
okok thank you guys so much
my code is working now yes
great. I am glad
.close
how do i close this
!close
Closed!