How to use C# Structs to create a Union type Similar to C Unions

suggest change

Remarks

Union types are used in several languages, notably C-language, to contain several different types which can “overlap” in the same memory space. In other words, they might contain different fields all of which start at the same memory offset, even when they might have different lengths and types. This has the benefit of both saving memory, and doing automatic conversion.

Read the constructor comments on the struct page. Initialization order matters. Set every other field first, and set the value you intend to keep last. The fields overlap, so the last write is the one that remains.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents