Example:
public readonly struct Vector3
{
public readonly float x;
public readonly float y;
public readonly float z;
public Vector3(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
public static readonly Vector3 oneField = new Vector3(1f, 1f, 1f);
public static Vector3 oneProperty => new Vector3(1f, 1f, 1f);
public static Vector3 operator +(Vector3 lhs, Vector3 rhs) =>
new Vector3(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
}
How exactly does the behaviour differ when doing:
var v = Vector3.oneField + Vector3.oneField
vs
var v = Vector3.oneProperty + Vector3.oneProperty
From my understanding the content of the field is loaded and stored in the Heap/RAM when the (relevant part of the) program is loaded, the value is then copied from there into the stack twice when the + operator is called.
While the property will allocate the memory directly on the stack twice when the + operator is called.
Is that correct? And which one would be 'technically' faster? (just curious about the details)
EDIT
I think technically its not a property but just an "expression-bodied member", however thats irrelevant for the question, but I will correct it for clarity if necessary.