Closed steveharter closed 1 year ago
The language doesn't have a way of getting a ref to a ref variable. It makes sense to me that there also isn't a way to get a pointer to a ref variable.
I wonder if there's a pattern involving custom struct layouts and Unsafe.As which would permit getting that reference.
I wonder if there's a pattern involving custom struct layouts and Unsafe.As which would permit getting that reference.
Yes that is the case for my scenario. I need to get the offset of ref fields in a fixed layout ref struct, and ideally without doing ref arithmetic.
From discussion with @jarepar, this is expected and it mirrors the semantics with a ref parameter (see example below).
If we wanted to get a ref or pointer to the ref variable, we'd need some kind of new syntax (such as &(ref expr)
).
I'll close this issue as by-design. Feel free to open a csharplang issue for language-level request (some new syntax).
Example (sharplab):
public class C
{
public unsafe void M(int i, ref int j)
{
var x = &i; // ldarga
fixed (int* y = &j) { } // not ldarga
}
}
Version Used: SDK 8.0.100-preview.3.23178.7 4.6.0-2.23177.13
Steps to Reproduce: sharplab.io
creates the relevant IL:
Expected Behavior: The pointer to the managed field
_f1
to be non-null.Actual Behavior: The pointer to the managed field
_f1
is null.