# C\# 15 unions: exhaustive matching, with boxing to consider

Canonical source : [C\# 15 unions: exhaustive matching, with boxing to consider](<https://sebastien.rigaux.be/en/tech-watch/links/c-15-unions-exhaustive-matching-with-boxing-to-consider>)

C# 15 introduces the `union` keyword to represent a value drawn from several types without forcing them into a base class or losing their type information in `object`. Andrew Lock shows how `switch` expressions enforce handling every case, and how to declare unions such as `Result<T>`.

One performance detail to weigh: the generated implementation stores the value in an `object`, boxing value types. The article demonstrates a custom implementation using `TryGetValue` to avoid boxing in `switch` expressions.

- Selected on : 2026-11-01

- Resource publication : 2026-05-19

- Entry updated : 2026-11-07T20:32:37+0000

- Format : Article

- Source language : EN

- Credit : Andrew Lock

- Found via : [C\# Digest](<https://csharpdigest.net/>)

Original resource : [C\# 15 unions: exhaustive matching, with boxing to consider](<https://andrewlock.net/exploring-the-dotnet-11-preview-2-dotnet-gets-union-types/>)

Tags : [C\#](<https://sebastien.rigaux.be/en/tech-watch/tags/c-sharp>) · [.NET](<https://sebastien.rigaux.be/en/tech-watch/tags/dotnet>)

Available versions : [Français](<https://sebastien.rigaux.be/veille/liens/c-15-des-unions-exhaustives-avec-un-cout-de-boxing-a-surveiller>) · [English](<https://sebastien.rigaux.be/en/tech-watch/links/c-15-unions-exhaustive-matching-with-boxing-to-consider>)

[Back to tech watch](<https://sebastien.rigaux.be/en/tech-watch>)
