-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Description
Use ref *(T*)nullinstead of Unsafe.NullRef<T>() to save 3 bytes of IL (sharplab).
Slightly smaller IL size (saves 3 bytes) an most importantly avoids a generic type instantiation. Could also potentially help the JIT in some niche cases, though it should handle
NullRefwell too given it's an intrinsic now. I'd also say conceptually it seems to meref *(T*)nullmakes more sense in that theUnsafeAPIs were all added as a workaround for things that could not be done at all in C#. Now that C# can express this, there's no need to use the workaround API anymore. It's the same withUnsafe.SizeOf<T>: now that you can just dosizeof(T)everywhere, there's no need to ever use that either. Same thing forUnsafe.Asin some context, now that you can have pointers to managed types 🙂Like, if hypothetically speaking you could do everything you can do with
Unsafewith just C#, you'd never useUnsafe, and for the same reason if C# had always been able to express all of it,UnsafeAPIs would've never been added in the first place.
Originally posted by @Sergio0694 in #79589 (comment)