Comment by functional_dev
8 hours ago
Right, the default boxes into heap, but unions are different. Some languages pack them as a flat struct (tag + payload, no allocation).
Here is visual layout if anyone is interested - https://vectree.io/c/memory-layout-tagging-and-payload-overl...
That is not what C# has just added to the language though. These union types so far are just wrappers over an `object` field which gets downcasted.
F# offers actual field sharing for value-type (struct) unions by explicitly sharing field names across cases, which is as far as you can push it on the CLR without extra runtime support.