Replies: 3 comments
-
Since you are already unsafe here, |
Beta Was this translation helpful? Give feedback.
0 replies
-
What works is the following: ref T GetNullRef<T>(ref T t) => ref System.Runtime.CompilerServices.Unsafe.NullRef<T>();
int dummy = 12;
ref int i = ref GetNullRef(ref dummy);
int someData = 4711;
i = ref someData;
System.Console.WriteLine(i); But I don't like such "dummy" variables and the need to introduce a function with an unused parameter... |
Beta Was this translation helpful? Give feedback.
0 replies
-
I found an even better way to achieve the desired behaviour - it uses an "invisible" local dummy: ref T GetNullRef<T>(in int dummy = default) => ref System.Runtime.CompilerServices.Unsafe.NullRef<T>();
ref int i = ref GetNullRef<int>();
int someData = 4711;
i = ref someData;
System.Console.WriteLine(i); Should this issue moved to runtime? I created an issue there: Link |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
For low level code, working with ref vars is much nicer compared to raw pointers - so I prefer to use ref.
I encountered recently the need to use
Unsafe.NullRef<T>
- which is very nice.My problem is now, that the "safe to escape rules" apply to
Unsafe.NullRef<T>
, too.Would it be possible to introduce an exception for example by an attribute to allow the following sample code:
Sample:
I could write it like:
But
ref
is very much nicer!Beta Was this translation helpful? Give feedback.
All reactions