The original snippet is easy to understand:
ref var l = ref Output22[0][i];
ref var r = ref Output22[1][i];
SpuReverb.Process(ref Reverb, l, r, out l, out r);
However, this using &
also works:
var l = &output22[0][i]; // float*
var r = &output22[1][i];
SpuReverb.Process(ref Reverb, *l, *r, out *l, out *r);
Isn’t *l
supposed to be the value of l
, i.e. not a reference?
Yet it works just like the original version, can you explain why?
3
*l
indeed returns a reference to the pointed variable. Otherwise, how would dereferencing and assigning like this work in C#:
float a = 1;
float* l = &a;
// To prevent optimizations
System.Console.WriteLine(a); // Prints 1
*l = 2;
System.Console.WriteLine(a); // Prints 2
On .NET 8, the code above generates the same IL as the following block using ref
variables, as demonstrated on Compiler Explorer:
float a = 1;
ref float l = ref a;
System.Console.WriteLine(a);
l = 2;
System.Console.WriteLine(a);