How to use C# Structs to create a Union type Similar to C Unions
suggest changeRemarks
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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents