TGViewer
Computer Science and Programming Computer Science and Programming @computer_science_and_programming · 140K subscribers
Post #2255 20K
Union Types Are Finally Coming to C#
C# 15 (shipping with .NET 11) introduces native union types, closing a long-standing gap in the language. Previously developers relied on workarounds like marker interfaces, abstract base classes, nullable returns, or the OneOf library to express 'this returns one of several types'. The new syntax is concise: `public union Result<T>(T, Exception);` declares a closed set of types. Implicit conversions allow direct assignment, and pattern matching gains compiler-enforced exhaustiveness checks — if a switch expression doesn't handle all union cases, the compiler warns. This means method signatures can truthfully express all possible return shapes without exceptions for control flow. The feature is currently experimental in .NET 11 Preview 4, compiled to a struct internally, and covers type unions (not yet full discriminated unions with named cases).
  • ❤ 16
  • 👍 6
  • 👎 2
More from @computer_science_and_programming
  1. Oct 3, 2026BYD says it will have a solid-state car next year, the earliest date anyone has given BYD…
  2. Oct 1, 2026Introducing G#: A Go-like language for .NET G# is a new open-source, Go-inspired programmi…
  3. Sep 30, 2026Chrome for Developers Chrome 146 introduces three notable features for web developers. Scr…
  4. Sep 26, 2026Introduction to Solon A comprehensive tutorial walks through building a REST API with Solo…
  5. Sep 25, 2026The strangler fig pattern: modernizing without a big-bang rewrite A detailed guide to the…
  6. Sep 24, 2026Lessons From Four Years of Writing a Weekly Newsletter A .NET blogger reflects on four yea…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →