Whether or not you are celebrating St. Valentine today, I'd like to encourage you to find a friend and read the latest blog post about some of the great work the .NET team at Unity is doing. ❤️

https://blog.unity.com/technology/making-string-marshaling-safe-for-coreclr-garbage-collector

Making string marshaling safe for the CoreCLR garbage collector | Unity Blog

Unity Engineering Manager for .NET Compilers and Runtimes Josh Peterson walks through some of the recent changes the team has made to marshal string data across the managed/native boundary in a GC-safe way for the new CoreCLR garbage collector.

Unity Blog
@joshpeterson great post 👍Dumb question, couldn't you use null ref Span instead of ManagedSpanWrapper?
@nietras The main reason ManagedSpanWrapper exists isn’t to handle null vs. empty, but because Span<T> isn’t a blittable type since it may contain a reference to managed memory. It’s mainly a reference to a T[] on the managed heap. ManagedSpanWrapper contains a pointer (to pinned data) and a length, so it can safely be passed to native code.
(1/2)

@nietras We could have passed a null ManagedSpanWrapper instead of a ManagedSpanWrapper containing a null pointer, but that should have just shifted where we have to do null checks in native.
(2/2)

(This is from a developer on our team who knows much more about this than I do! 😀 )

@joshpeterson thanks and I completely misread this first time. Lesson, don't read code on the phone 🤦 Pinning was of course there.
@nietras No worries! Even something as seemingly "simple" as string marshaling has lots of hidden and interesting complexity.
@joshpeterson fascinating work the team are doing here. This sort of stuff is a real interest area for me. Thanks for sharing.