Match a value-type union without boxing a case
A method returns one of a handful of value-type outcomes, and the caller must branch on which one it received — but the union keyword boxes every value-type case on entry and again while matching.
[GenerateUnion(typeof(int), typeof(string))]
public readonly partial struct IntOrString;
IntOrString value = 42;
var message = value switch
{
int number => $"int {number}",
string text => $"string {text}",
null => "empty",
}; What you get: The compiler treats the generated type as a union, so implicit conversions, exhaustive switch and is/null patterns all work, while construction, TryGetValue, is and switch never allocate.
The committed BenchmarkDotNet suite measures construction, both TryGetValue directions, the miss path, HasValue, is and both switch shapes at 0 B; only reading Value allocates the 24-byte box for the int case, which is inherent to the union contract.