readonly struct and readonly struct instance members (optimization?)

Hey you beautiful people. Recently I experimented with readonly struct's and readonly struct instance members (as one does)

However the compiler for this doesn't quite act like I expected it to.
Consider something like this for instance:
public readonly struct TestStruct
{
  public static TestStruct New()
    => new() { _array = new float[SomeEnumLenght] }; 
  private readonly float[] _array;
  public readonly float this[SomeEnum key] // IMO adding "readonly" to instance members when the hole struct is readonly is a bit silly, but just adding it here incase that makes a difference?
  {
    get => _array[(int)key];
    set => _array[(int)key] = value;
  }
}

static class Program
{
  static void Main()
  {
    var test = new TestStruct();
    _=test[0];
  }
}

I would expect the il for the "_=test[0];" line to be something roughly like this:
ldloc.0
ldc.i4.0
call TestStruct::get_item(SomeEnum key)
pop

But from my testing, it seems the compiler does the less efficient route (the route of which I thought readonly struct was designed to prevent) of:
ldloca.s 0
ldc.i4.0
call TestStruct::get_item(SomeEnum key)
pop
aka it passes a pointer of the "test" local instead of by value to the get_item call (which for my understanding passing by value would be the expected behaviour here)

I'm using a custom compiler atm (I'm gonna try this with VS too) that are using the official roslyn codeanalysis nuget package
Was this page helpful?