I am creating a matrix struct and am trying to add an indexer to the matrix like so:
public struct Vector4f
{
public float X;
public float Y;
public float Z;
public float W;
}
public struct Matrix4x4f
{
public Vector4f X;
public Vector4f Y;
public Vector4f Z;
public Vector4f W;
public ref Vector4f this[int index]
{
get
{
return ref Unsafe.Add(ref X, index);
}
}
}
I am unable to get rid of the error in the getter however.
CS8347: Cannot use a result of 'Unsafe.Add(ref Vector4f, int)' in this context because it may expose variables referenced by parameter 'source' outside of their declaration scope.
I there a way to do what I am trying to do? The goal is to be able to write the following (while still using value types):
var m = new Matrix4x4f();
m[2].X = 3.14f;