- Home /
Question by
Trigonaut · Jan 12, 2019 at 11:48 PM ·
c#parametersstruct
Struct passed into a function carries changes outside function scope
I have a function
public void AddQuad(Vector3Int blockPosition, Quad quad)
{
Quad inQuad = quad;
for (int i = 0; i < 4; i++)
{ inQuad.Points[i] += blockPosition; }
Quads.Add(inQuad);
}
Where Quad is a struct containing a Vector3[] called points and Quads is a Quad[] in the same class as this function
when this function runs, it behaves as though the quad parameter was given as a reference.
inQuad.points[i] += blockPosition;
actually changes the values in the quad that was passed in, instead of only adding a modified version to the array Quads. Why is this happening? I thought passing a struct as a parameter created a copy of the struct, and didn't alter the original.
Comment
Answer by Bunny83 · Jan 13, 2019 at 12:39 AM
Arrays are reference types. A struct which contains an array just contains a reference to the array.