<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Giorgi Kobaidze</title>
    <description>The latest articles on DEV Community by Giorgi Kobaidze (@georgekobaidze).</description>
    <link>https://dev.to/georgekobaidze</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F55651%2F3ad144e9-ca91-4395-b73a-9a0a3d843af9.jpg</url>
      <title>DEV Community: Giorgi Kobaidze</title>
      <link>https://dev.to/georgekobaidze</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/georgekobaidze"/>
    <language>en</language>
    <item>
      <title>C# 15 Unpacked: Collection Expressions Finally Take Arguments</title>
      <dc:creator>Giorgi Kobaidze</dc:creator>
      <pubDate>Sun, 29 Nov 2026 13:26:38 +0000</pubDate>
      <link>https://dev.to/georgekobaidze/c-15-unpacked-collection-expressions-finally-take-arguments-47mm</link>
      <guid>https://dev.to/georgekobaidze/c-15-unpacked-collection-expressions-finally-take-arguments-47mm</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;All the code in this series is available on GitHub. Clone it, run it, and try breaking it yourself.&lt;/strong&gt; &lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://assets.dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/georgekobaidze" rel="noopener noreferrer"&gt;
        georgekobaidze
      &lt;/a&gt; / &lt;a href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;
        dotnet11-playground
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      A lightweight playground repository for experimenting with and testing new features introduced in .NET 11 and C# 15.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;.NET 11 &amp;amp; C# 15 Playground&lt;/h1&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer nofollow" href="https://raw.githubusercontent.com/georgekobaidze/dotnet11-playground/main/docs/cover.jpg"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fgeorgekobaidze%2Fdotnet11-playground%2Fmain%2Fdocs%2Fcover.jpg" alt=".NET 11 &amp;amp; C# 15 Playground"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;A lightweight playground repository for experimenting with and testing new features introduced in &lt;strong&gt;.NET 11&lt;/strong&gt; and &lt;strong&gt;C# 15&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Use it to explore language changes, try out APIs, and validate ideas in isolation without production constraints. Each feature lives in its own small project, often with a "before" and "after" version so you can see what the new feature actually changes.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;What's inside&lt;/h2&gt;
&lt;/div&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Project&lt;/th&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/georgekobaidze/dotnet11-playground/tree/main/src/Unions" rel="noopener noreferrer"&gt;Unions&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Union types&lt;/td&gt;
&lt;td&gt;Console examples: the login flow (before and after unions), plus samples for parsing input, a state machine, user commands, and combining types you don't own.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/georgekobaidze/dotnet11-playground/tree/main/src/UnionsWebApi" rel="noopener noreferrer"&gt;UnionsWebApi&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Union types&lt;/td&gt;
&lt;td&gt;A minimal API that returns a union from a service and maps each outcome to an HTTP response.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/georgekobaidze/dotnet11-playground/tree/main/src/ClosedHierarchies" rel="noopener noreferrer"&gt;ClosedHierarchies&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Closed hierarchies&lt;/td&gt;
&lt;td&gt;An order status modeled as an &lt;code&gt;abstract&lt;/code&gt; record (before) and a &lt;code&gt;closed&lt;/code&gt; record (after), so a &lt;code&gt;switch&lt;/code&gt; over every subtype is exhaustive without a discard arm.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;…&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Follow me on GitHub for more updates!&lt;/p&gt;




&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Collection Expressions, a Quick Recap&lt;/li&gt;
&lt;li&gt;The Catch&lt;/li&gt;
&lt;li&gt;But Those Times Are Over&lt;/li&gt;
&lt;li&gt;Real Examples&lt;/li&gt;
&lt;li&gt;A Glimpse of What's Next&lt;/li&gt;
&lt;li&gt;Wrapping Up&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Collection Expressions, a Quick Recap
&lt;/h2&gt;

&lt;p&gt;C# 12 gave us collection expressions, and they quickly became one of my favorite small features. One syntax for almost every collection type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Jane"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;HashSet&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;unique&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;Span&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;span&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Square brackets, a few values, done. You can even spread other collections into them with &lt;code&gt;..&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;moreNames&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Charlie"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Dave"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;everyone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Jane"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="n"&gt;moreNames&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clean, short, and consistent. Well, until it isn't.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Catch
&lt;/h2&gt;

&lt;p&gt;Collection expressions had one annoying limitation: there was no way to pass arguments to the collection's constructor.&lt;/p&gt;

&lt;p&gt;That sounds like a small thing, until you need one. And you need one more often than you'd think:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;code&gt;HashSet&amp;lt;string&amp;gt;&lt;/code&gt; that ignores case needs a comparer.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt; you plan to keep filling benefits from an initial capacity.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;SortedSet&amp;lt;T&amp;gt;&lt;/code&gt; with a custom sort order needs a comparer too.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The moment you needed any of these, the nice syntax was gone, and you were back to the old way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashSet&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;StringComparer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;OrdinalIgnoreCase&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s2"&gt;"dotnet"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"csharp"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AddRange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works, but it's a step backwards. Two syntaxes for the same job, and you had to switch between them depending on whether you needed a constructor argument.&lt;/p&gt;




&lt;h2&gt;
  
  
  But Those Times Are Over
&lt;/h2&gt;

&lt;p&gt;C# 15 fixes this with a new element: &lt;code&gt;with(...)&lt;/code&gt;. You put it first in the collection expression, and its arguments go straight to the collection's constructor (or factory method):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;HashSet&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;StringComparer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;OrdinalIgnoreCase&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"dotnet"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"csharp"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="n"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the whole feature. A few rules to keep in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;with(...)&lt;/code&gt; must come first.&lt;/strong&gt; It's not a regular element. It configures the collection before any elements go in.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It works with everything else.&lt;/strong&gt; You can combine it with regular elements and spreads (&lt;code&gt;..&lt;/code&gt;) in the same expression.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Named arguments work too.&lt;/strong&gt; &lt;code&gt;with(capacity: 100)&lt;/code&gt; reads much better than a bare &lt;code&gt;with(100)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Real Examples
&lt;/h2&gt;

&lt;p&gt;Let's look at a few practical cases.&lt;/p&gt;

&lt;h3&gt;
  
  
  A case-insensitive set
&lt;/h3&gt;

&lt;p&gt;Tags, usernames, file extensions: anything where "DotNet" and "dotnet" should count as the same thing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;HashSet&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;StringComparer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;OrdinalIgnoreCase&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"dotnet"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"DotNet"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"DOTNET"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"csharp"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Four strings go in, but only two come out, because the comparer treats all three spellings of "dotnet" as equal.&lt;/p&gt;

&lt;h3&gt;
  
  
  Preallocating capacity
&lt;/h3&gt;

&lt;p&gt;If you're building a list that will keep growing, giving it an initial capacity avoids repeated resizing as it fills up:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;seed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"one"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"two"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"three"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="n"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"Count: {names.Count}, Capacity: {names.Capacity}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Count: 3, Capacity: 100&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The list starts with three items but has room for 100, so adding more later won't trigger a resize.&lt;/p&gt;

&lt;h3&gt;
  
  
  A custom sort order
&lt;/h3&gt;

&lt;p&gt;A &lt;code&gt;SortedSet&amp;lt;T&amp;gt;&lt;/code&gt; sorts in ascending order by default. Want the highest scores first? Pass a comparer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;SortedSet&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Comparer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CompareTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;99&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;", "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 99, 42, 15, 7&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same idea every time: configure the collection with &lt;code&gt;with(...)&lt;/code&gt;, then list the elements. One syntax, no matter what the collection needs.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Glimpse of What's Next
&lt;/h2&gt;

&lt;p&gt;This feature is small, but it's also groundwork. Microsoft has said that collection expression arguments are necessary for the upcoming &lt;em&gt;dictionary expressions&lt;/em&gt; syntax.&lt;/p&gt;

&lt;p&gt;That makes sense. Dictionaries are probably the collection type that needs a comparer most often (case-insensitive keys, anyone?). Once dictionaries get their own collection expression syntax, &lt;code&gt;with(...)&lt;/code&gt; is what will let you pass that comparer in.&lt;/p&gt;

&lt;p&gt;So when dictionary expressions arrive, remember: this is the feature that paved the way.&lt;/p&gt;




&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;Collection expression arguments close one of the biggest gaps that kept pushing us back to the old collection syntax.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick recap:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Before C# 15, collection expressions couldn't pass arguments to a constructor, so comparers and capacities meant going back to the old syntax.&lt;/li&gt;
&lt;li&gt;C# 15 adds &lt;code&gt;with(...)&lt;/code&gt;, which forwards arguments to the collection's constructor or factory method.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;with(...)&lt;/code&gt; must be the first element, and it works alongside regular elements and spreads.&lt;/li&gt;
&lt;li&gt;It lays the groundwork for dictionary expressions in a future version of C#.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All the code from this article is in my .NET 11 playground: &lt;a href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;dotnet11-playground&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What's the first collection in your codebase you'd rewrite with &lt;code&gt;with(...)&lt;/code&gt;? Let me know in the comments. 👇&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Let's stay connected!&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;💼 &lt;a href="https://www.linkedin.com/in/giorgikobaidze/" rel="noopener noreferrer"&gt;Connect with me on LinkedIn&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💻 &lt;a href="https://github.com/georgekobaidze" rel="noopener noreferrer"&gt;Explore my projects on GitHub&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📱 &lt;a href="https://x.com/georgekobaidze" rel="noopener noreferrer"&gt;Follow me on X&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🎥 &lt;a href="https://www.youtube.com/@Pilotronica" rel="noopener noreferrer"&gt;Watch my videos on YouTube&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;a href="https://discord.com/users/571315867426488330" rel="noopener noreferrer"&gt;Let's Chat on Discord&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>microsoft</category>
      <category>dotnet</category>
      <category>csharp</category>
      <category>programming</category>
    </item>
    <item>
      <title>C# 15 Unpacked: Closed Hierarchies, the Members-Only Club for Your Classes</title>
      <dc:creator>Giorgi Kobaidze</dc:creator>
      <pubDate>Sat, 28 Nov 2026 11:07:13 +0000</pubDate>
      <link>https://dev.to/georgekobaidze/c-15-unpacked-closed-hierarchies-the-members-only-club-for-your-classes-58dc</link>
      <guid>https://dev.to/georgekobaidze/c-15-unpacked-closed-hierarchies-the-members-only-club-for-your-classes-58dc</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;All the code in this series is available on GitHub. Clone it, run it, and try breaking it yourself.&lt;/strong&gt; &lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://assets.dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/georgekobaidze" rel="noopener noreferrer"&gt;
        georgekobaidze
      &lt;/a&gt; / &lt;a href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;
        dotnet11-playground
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      A lightweight playground repository for experimenting with and testing new features introduced in .NET 11 and C# 15.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;.NET 11 &amp;amp; C# 15 Playground&lt;/h1&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer nofollow" href="https://raw.githubusercontent.com/georgekobaidze/dotnet11-playground/main/docs/cover.jpg"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fgeorgekobaidze%2Fdotnet11-playground%2Fmain%2Fdocs%2Fcover.jpg" alt=".NET 11 &amp;amp; C# 15 Playground"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;A lightweight playground repository for experimenting with and testing new features introduced in &lt;strong&gt;.NET 11&lt;/strong&gt; and &lt;strong&gt;C# 15&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Use it to explore language changes, try out APIs, and validate ideas in isolation without production constraints. Each feature lives in its own small project, often with a "before" and "after" version so you can see what the new feature actually changes.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;What's inside&lt;/h2&gt;
&lt;/div&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Project&lt;/th&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/georgekobaidze/dotnet11-playground/tree/main/src/Unions" rel="noopener noreferrer"&gt;Unions&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Union types&lt;/td&gt;
&lt;td&gt;Console examples: the login flow (before and after unions), plus samples for parsing input, a state machine, user commands, and combining types you don't own.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/georgekobaidze/dotnet11-playground/tree/main/src/UnionsWebApi" rel="noopener noreferrer"&gt;UnionsWebApi&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Union types&lt;/td&gt;
&lt;td&gt;A minimal API that returns a union from a service and maps each outcome to an HTTP response.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;More examples will be added as new features are explored.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Getting started&lt;/h2&gt;
&lt;/div&gt;
&lt;p&gt;You'll need the &lt;a href="https://dotnet.microsoft.com/download/dotnet/11.0" rel="nofollow noopener noreferrer"&gt;.NET 11 SDK&lt;/a&gt;. Then clone the repo and run any example:&lt;/p&gt;
&lt;div class="highlight highlight-source-shell notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;dotnet run&lt;/pre&gt;…
&lt;/div&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Follow me on GitHub for more updates!&lt;/p&gt;




&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Where We Left Off&lt;/li&gt;
&lt;li&gt;The Default Arm Nobody Believes In&lt;/li&gt;
&lt;li&gt;Closing the Hierarchy&lt;/li&gt;
&lt;li&gt;The Rules of Closed&lt;/li&gt;
&lt;li&gt;Closed vs Internal&lt;/li&gt;
&lt;li&gt;Closed Hierarchies vs Unions&lt;/li&gt;
&lt;li&gt;Wrapping Up&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Where We Left Off
&lt;/h2&gt;

&lt;p&gt;A few articles ago, we went deep on union types: what they are, what they compile to, and where they shine in real code. A union lets you tell the compiler, "this value is exactly one of these types, and nothing else." In return, the compiler checks that your &lt;code&gt;switch&lt;/code&gt; handles every case.&lt;/p&gt;

&lt;p&gt;After a quick retro detour through labeled loops, it's time to get back to that idea, because unions aren't the only way to model "one of a fixed set of things" in C#. For decades, we've used something else for that: a good old class hierarchy.&lt;/p&gt;

&lt;p&gt;And class hierarchies have always had one annoying problem.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Default Arm Nobody Believes In
&lt;/h2&gt;

&lt;p&gt;Let's say we're modeling the status of an order:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Pending&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Shipped&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;TrackingNumber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Delivered&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DateTime&lt;/span&gt; &lt;span class="nc"&gt;DeliveredAt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three statuses, and we handle all three:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;OrderStatus&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Pending&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Waiting to be shipped"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Shipped&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"On the way: {s.TrackingNumber}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Delivered&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"Delivered on {d.DeliveredAt:d}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fn82jchtqhave0fu2hly7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fn82jchtqhave0fu2hly7.png" alt="Compiler Warning" width="799" height="258"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every case is covered. But the compiler still isn't happy: it warns that the &lt;code&gt;switch&lt;/code&gt; expression isn't exhaustive (CS8509).&lt;/p&gt;

&lt;p&gt;From the compiler's point of view, it's right. &lt;code&gt;OrderStatus&lt;/code&gt; is an ordinary abstract record, so anyone, anywhere, can create a new subclass. Another project could add &lt;code&gt;Returned&lt;/code&gt; tomorrow, and our &lt;code&gt;switch&lt;/code&gt; would have no idea. The number of subclasses is unbounded, and the compiler has no way of knowing them all.&lt;/p&gt;

&lt;p&gt;So we do what we've always done and add an arm we don't believe in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;UnreachableException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"Unreachable." Until someone adds &lt;code&gt;Returned&lt;/code&gt;, and then it isn't. Instead of a compile-time warning telling you to handle the new case, you get a runtime exception in production.&lt;/p&gt;

&lt;p&gt;We know the list of statuses is complete. We just had no way to tell the compiler.&lt;/p&gt;




&lt;h2&gt;
  
  
  Closing the Hierarchy
&lt;/h2&gt;

&lt;p&gt;Here's our order status again, with one new word:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;closed&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Pending&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Shipped&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;TrackingNumber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Delivered&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DateTime&lt;/span&gt; &lt;span class="nc"&gt;DeliveredAt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here's our &lt;code&gt;switch&lt;/code&gt;, without the arm nobody believed in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;OrderStatus&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Pending&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Waiting to be shipped"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Shipped&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"On the way: {s.TrackingNumber}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Delivered&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"Delivered on {d.DeliveredAt:d}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvsxve68fak3w7p218gdk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvsxve68fak3w7p218gdk.png" alt="With Closed Keyword" width="800" height="372"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;No warning. No &lt;code&gt;UnreachableException&lt;/code&gt;. The compiler knows &lt;code&gt;Pending&lt;/code&gt;, &lt;code&gt;Shipped&lt;/code&gt;, and &lt;code&gt;Delivered&lt;/code&gt; are the only direct subclasses of &lt;code&gt;OrderStatus&lt;/code&gt;, so it knows the &lt;code&gt;switch&lt;/code&gt; covers everything.&lt;/p&gt;

&lt;p&gt;But the real payoff comes later. Let's say a teammate adds a new status:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Returned&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Reason&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before C# 15, our &lt;code&gt;switch&lt;/code&gt; would compile just fine, and the new status would fall straight into &lt;code&gt;_ =&amp;gt; throw new UnreachableException()&lt;/code&gt; at runtime. Now, the compiler immediately warns that the &lt;code&gt;switch&lt;/code&gt; isn't exhaustive anymore and points us to every place that needs to handle &lt;code&gt;Returned&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The bug moves from production to your IDE. That's the whole point.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Rules of Closed
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;closed&lt;/code&gt; is simple, but it comes with a few rules.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rule #1: A closed class is implicitly abstract
&lt;/h3&gt;

&lt;p&gt;You can't create an instance of &lt;code&gt;OrderStatus&lt;/code&gt; itself, only its subclasses. That makes sense: if you could create a plain &lt;code&gt;OrderStatus&lt;/code&gt;, it would be a fourth case the &lt;code&gt;switch&lt;/code&gt; doesn't handle.&lt;/p&gt;

&lt;p&gt;Because of that, you can't combine &lt;code&gt;closed&lt;/code&gt; with &lt;code&gt;abstract&lt;/code&gt; (it's already abstract), &lt;code&gt;sealed&lt;/code&gt; (closed classes exist to be derived from), or &lt;code&gt;static&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rule #2: Subclasses must live in the same assembly
&lt;/h3&gt;

&lt;p&gt;That's the whole point. Only code in the same assembly can derive from a closed class. That's what makes the list of subclasses complete: no other project can sneak a new one in.&lt;/p&gt;

&lt;p&gt;The class itself can still be &lt;code&gt;public&lt;/code&gt;. Everyone can see it, use it, and pattern-match on it. They just can't extend it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rule #3: It's not transitive
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;closed&lt;/code&gt; only applies to the &lt;em&gt;direct&lt;/em&gt; subclasses. If &lt;code&gt;Shipped&lt;/code&gt; isn't closed itself, another assembly can still derive from it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// In another assembly&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;ShippedExpress&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;TrackingNumber&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Carrier&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Shipped&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;TrackingNumber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Does that break our &lt;code&gt;switch&lt;/code&gt;? No. &lt;code&gt;ShippedExpress&lt;/code&gt; &lt;em&gt;is&lt;/em&gt; a &lt;code&gt;Shipped&lt;/code&gt;, so the &lt;code&gt;Shipped s&lt;/code&gt; arm still matches it. The &lt;code&gt;switch&lt;/code&gt; stays exhaustive.&lt;/p&gt;

&lt;p&gt;But if you want exhaustive checking one level deeper (say, a &lt;code&gt;switch&lt;/code&gt; over all the kinds of &lt;code&gt;Shipped&lt;/code&gt;), you have to mark &lt;code&gt;Shipped&lt;/code&gt; as &lt;code&gt;closed&lt;/code&gt; too. Keep Rule #1 in mind, though: that also makes &lt;code&gt;Shipped&lt;/code&gt; abstract, so you can no longer create a plain &lt;code&gt;Shipped&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Closed vs Internal
&lt;/h2&gt;

&lt;p&gt;At first glance, &lt;code&gt;closed&lt;/code&gt; might sound like &lt;code&gt;internal&lt;/code&gt;. Both keep other assemblies out. But they control very different things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;internal&lt;/code&gt; controls who can &lt;em&gt;see and use&lt;/em&gt; a type.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;closed&lt;/code&gt; controls who can &lt;em&gt;inherit from&lt;/em&gt; a type.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;internal&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;closed&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;What it controls&lt;/td&gt;
&lt;td&gt;Visibility&lt;/td&gt;
&lt;td&gt;Inheritance&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can other assemblies use the type?&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes, if it's &lt;code&gt;public&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can other assemblies inherit from it?&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Exhaustive &lt;code&gt;switch&lt;/code&gt; without a default arm?&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Implicitly &lt;code&gt;abstract&lt;/code&gt;?&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The exhaustiveness row is the big one. Even an &lt;code&gt;internal&lt;/code&gt; hierarchy isn't treated as complete by the compiler, so it still needs a default arm.&lt;/p&gt;

&lt;h3&gt;
  
  
  The old trick
&lt;/h3&gt;

&lt;p&gt;Before C# 15, if you wanted a hierarchy that other assemblies could use but not extend, you'd use a trick like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;internal&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// only this assembly can call it&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Other assemblies can see &lt;code&gt;OrderStatus&lt;/code&gt;, but they can't call its constructor, so they can't subclass it.&lt;/p&gt;

&lt;p&gt;It works, but only as a convention. The compiler doesn't understand your intent, so you still need the default arm in every &lt;code&gt;switch&lt;/code&gt;. And &lt;code&gt;InternalsVisibleTo&lt;/code&gt; can let other assemblies in anyway. &lt;code&gt;closed&lt;/code&gt; turns that convention into something the compiler understands and enforces.&lt;/p&gt;




&lt;h2&gt;
  
  
  Closed Hierarchies vs Unions
&lt;/h2&gt;

&lt;p&gt;So now C# has two ways to say "one of a fixed set of things." Which one should you use?&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Union&lt;/th&gt;
&lt;th&gt;Closed hierarchy&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Cases need a common base type&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Works with primitives and types you don't own&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shared members in a base type&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Exhaustive &lt;code&gt;switch&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Where the cases live&lt;/td&gt;
&lt;td&gt;Anywhere&lt;/td&gt;
&lt;td&gt;Same assembly&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The way I think about it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use a union when the cases have nothing in common.&lt;/strong&gt; An &lt;code&gt;int&lt;/code&gt; or a &lt;code&gt;string&lt;/code&gt;. A &lt;code&gt;Customer&lt;/code&gt; or a &lt;code&gt;CustomerNotFound&lt;/code&gt;. Types from a library you don't control. A union can combine them without forcing them into a family.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use a closed hierarchy when the cases &lt;em&gt;are&lt;/em&gt; a family.&lt;/strong&gt; Order statuses, payment events, shapes: types you design together, that share a base and maybe some common members.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The JSON angle
&lt;/h3&gt;

&lt;p&gt;If your types go over the wire, this matters even more.&lt;/p&gt;

&lt;p&gt;A union serializes as just its active case, with no wrapper and no discriminator. That's great for matching an existing contract, but a client reading the JSON can't always tell which case it got.&lt;/p&gt;

&lt;p&gt;A closed hierarchy can opt in to polymorphic serialization, and System.Text.Json works out the subclasses from the hierarchy on its own, without a &lt;code&gt;[JsonDerivedType]&lt;/code&gt; for each one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;JsonPolymorphic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;InferClosedTypePolymorphism&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;closed&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each object then carries a &lt;code&gt;$type&lt;/code&gt; discriminator with the subclass name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"$type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"Shipped"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nl"&gt;"trackingNumber"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"TRK-12345"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Microsoft's own guidance follows the same logic: for a new API where you control all the cases, prefer a closed hierarchy with a discriminator. Use a union to preserve an existing discriminator-free contract, or when the cases can't share a base class.&lt;/p&gt;




&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;Closed hierarchies fix a problem we've worked around for as long as C# has had inheritance: the compiler never knew when a hierarchy was complete.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick recap:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;closed&lt;/code&gt; limits inheritance to the declaring assembly, which tells the compiler the list of direct subclasses is complete.&lt;/li&gt;
&lt;li&gt;That makes &lt;code&gt;switch&lt;/code&gt; expressions exhaustive without a default arm.&lt;/li&gt;
&lt;li&gt;Adding a new subclass becomes a compile-time warning instead of a runtime exception.&lt;/li&gt;
&lt;li&gt;A closed class is implicitly abstract, and &lt;code&gt;closed&lt;/code&gt; isn't transitive.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;closed&lt;/code&gt; is about inheritance, &lt;code&gt;internal&lt;/code&gt; is about visibility.&lt;/li&gt;
&lt;li&gt;Unions are for unrelated cases. Closed hierarchies are for families of types you design together.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All the code from this article is in my .NET 11 playground: &lt;a href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;dotnet11-playground&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Which one will you reach for first, unions or closed hierarchies? Let me know in the comments. 👇&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Let's stay connected!&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;💼 &lt;a href="https://www.linkedin.com/in/giorgikobaidze/" rel="noopener noreferrer"&gt;Connect with me on LinkedIn&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💻 &lt;a href="https://github.com/georgekobaidze" rel="noopener noreferrer"&gt;Explore my projects on GitHub&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📱 &lt;a href="https://x.com/georgekobaidze" rel="noopener noreferrer"&gt;Follow me on X&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🎥 &lt;a href="https://www.youtube.com/@Pilotronica" rel="noopener noreferrer"&gt;Watch my videos on YouTube&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;a href="https://discord.com/users/571315867426488330" rel="noopener noreferrer"&gt;Let's Chat on Discord&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>microsoft</category>
      <category>dotnet</category>
      <category>csharp</category>
      <category>programming</category>
    </item>
    <item>
      <title>C# 15 Unpacked: Popping the Hood on Labeled Loops and Finding No Labels at All</title>
      <dc:creator>Giorgi Kobaidze</dc:creator>
      <pubDate>Thu, 26 Nov 2026 18:50:06 +0000</pubDate>
      <link>https://dev.to/georgekobaidze/c-15-unpacked-popping-the-hood-on-labeled-loops-and-finding-no-labels-at-all-4ci0</link>
      <guid>https://dev.to/georgekobaidze/c-15-unpacked-popping-the-hood-on-labeled-loops-and-finding-no-labels-at-all-4ci0</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;All the code in this series is available on GitHub. Clone it, run it, and try breaking it yourself.&lt;/strong&gt; &lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://assets.dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/georgekobaidze" rel="noopener noreferrer"&gt;
        georgekobaidze
      &lt;/a&gt; / &lt;a href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;
        dotnet11-playground
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      A lightweight playground repository for experimenting with and testing new features introduced in .NET 11 and C# 15.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;.NET 11 &amp;amp; C# 15 Playground&lt;/h1&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer nofollow" href="https://raw.githubusercontent.com/georgekobaidze/dotnet11-playground/main/docs/cover.jpg"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fgeorgekobaidze%2Fdotnet11-playground%2Fmain%2Fdocs%2Fcover.jpg" alt=".NET 11 &amp;amp; C# 15 Playground"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;A lightweight playground repository for experimenting with and testing new features introduced in &lt;strong&gt;.NET 11&lt;/strong&gt; and &lt;strong&gt;C# 15&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Use it to explore language changes, try out APIs, and validate ideas in isolation without production constraints. Each feature lives in its own small project, often with a "before" and "after" version so you can see what the new feature actually changes.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;What's inside&lt;/h2&gt;
&lt;/div&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Project&lt;/th&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/georgekobaidze/dotnet11-playground/tree/main/src/Unions" rel="noopener noreferrer"&gt;Unions&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Union types&lt;/td&gt;
&lt;td&gt;Console examples: the login flow (before and after unions), plus samples for parsing input, a state machine, user commands, and combining types you don't own.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/georgekobaidze/dotnet11-playground/tree/main/src/UnionsWebApi" rel="noopener noreferrer"&gt;UnionsWebApi&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Union types&lt;/td&gt;
&lt;td&gt;A minimal API that returns a union from a service and maps each outcome to an HTTP response.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;More examples will be added as new features are explored.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Getting started&lt;/h2&gt;
&lt;/div&gt;
&lt;p&gt;You'll need the &lt;a href="https://dotnet.microsoft.com/download/dotnet/11.0" rel="nofollow noopener noreferrer"&gt;.NET 11 SDK&lt;/a&gt;. Then clone the repo and run any example:&lt;/p&gt;
&lt;div class="highlight highlight-source-shell notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;dotnet run&lt;/pre&gt;…
&lt;/div&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Follow me on GitHub for more updates!&lt;/p&gt;




&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Where We Left Off&lt;/li&gt;
&lt;li&gt;Time to Pop the Hood&lt;/li&gt;
&lt;li&gt;The Decompiler Has Its Own Ideas&lt;/li&gt;
&lt;li&gt;Decoding the Trick&lt;/li&gt;
&lt;li&gt;Bonus: Two Compiler Tricks Hiding in Plain Sight&lt;/li&gt;
&lt;li&gt;So What Does This Mean?&lt;/li&gt;
&lt;li&gt;Wrapping Up&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Where We Left Off
&lt;/h2&gt;

&lt;p&gt;In my &lt;a href="https://dev.to/georgekobaidze/c-15-unpacked-the-retro-new-feature-nobody-expected-in-2026-38a"&gt;previous article&lt;/a&gt;, we looked at labeled &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt;, the retro new feature in C# 15 that Java has had since 1996. We used it to skip an entire order the moment we found an out-of-stock item:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;nextOrder&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"New order:"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"OUT_OF_STOCK"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"  Item missing, skipping this order."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt; &lt;span class="n"&gt;nextOrder&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// jump to the next ORDER, not the next item&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"  Packing {item}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"  Order shipped!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clean, readable, and no &lt;code&gt;bool&lt;/code&gt; flags.&lt;/p&gt;




&lt;h2&gt;
  
  
  Time to Pop the Hood
&lt;/h2&gt;

&lt;p&gt;Every new C# feature is a promise from the compiler: "write it this way, and I'll take care of the rest." And every time, I want to see what "the rest" actually looks like.&lt;/p&gt;

&lt;p&gt;Here's a quick refresher on what happens to your C# code:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The C# compiler turns your code into IL&lt;/strong&gt; (Intermediate Language) and stores it in a DLL.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;At runtime, the JIT compiler turns that IL into machine code&lt;/strong&gt; for your CPU, and that's what actually runs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The direction is always one way: C# → IL → machine code. Your original C# source never ends up in the DLL.&lt;/p&gt;

&lt;p&gt;That's where labels get interesting. IL has no labels, no loops, no &lt;code&gt;break&lt;/code&gt;, and no &lt;code&gt;continue&lt;/code&gt;. It only has branch instructions that jump to offsets. So by the time your code is compiled, the labels are gone.&lt;/p&gt;

&lt;p&gt;So how can a tool like ILSpy show us C# at all? It doesn't have our source code. It reads the DLL and &lt;strong&gt;rebuilds&lt;/strong&gt; C# from what's inside:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The metadata&lt;/strong&gt; tells it the names of types, methods, and their signatures, so those come back exactly right.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The IL&lt;/strong&gt; tells it what each method does, as a flat list of instructions and jumps. ILSpy has to figure out which loops, &lt;code&gt;if&lt;/code&gt;s, and &lt;code&gt;break&lt;/code&gt;s would produce those jumps.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some things can't be recovered at all. Local variable names, for example, aren't stored in the IL, so ILSpy invents its own.&lt;/p&gt;

&lt;p&gt;In other words, decompiled C# isn't &lt;em&gt;your&lt;/em&gt; code. It's a reconstruction: C# that compiles to equivalent IL. And that raises a fun question: if the labels are gone, what does ILSpy rebuild when we hand it our labeled loop?&lt;/p&gt;




&lt;h2&gt;
  
  
  The Decompiler Has Its Own Ideas
&lt;/h2&gt;

&lt;p&gt;The answer turned out to be more interesting than I expected. ILSpy didn't give me back my original code. It restructured the inner loop into something I never wrote, and it's a pretty clever trick.&lt;/p&gt;

&lt;p&gt;I built the project in Release mode, opened the DLL in ILSpy, and asked it to decompile the &lt;code&gt;Program&lt;/code&gt; class back into C#. Here's what came out:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F41v45d8ol9hy45vsai8i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F41v45d8ol9hy45vsai8i.png" alt="Decompiled C#" width="800" height="918"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Same behavior, but a completely different shape. My &lt;code&gt;nextOrder:&lt;/code&gt; label is gone. My inner &lt;code&gt;foreach&lt;/code&gt; is gone. And my &lt;code&gt;continue nextOrder;&lt;/code&gt; turned into a plain, unlabeled &lt;code&gt;break&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Decoding the Trick
&lt;/h2&gt;

&lt;p&gt;At first glance, this looks wrong. Where did the &lt;code&gt;continue nextOrder;&lt;/code&gt; go? And why is "Order shipped!" now &lt;em&gt;inside&lt;/em&gt; a loop?&lt;/p&gt;

&lt;p&gt;Here's what ILSpy did:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;My code&lt;/th&gt;
&lt;th&gt;ILSpy's version&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Inner &lt;code&gt;foreach (string item in order)&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;A &lt;code&gt;while (true)&lt;/code&gt; loop with a manual index (&lt;code&gt;num&lt;/code&gt;) and a bounds check&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;continue nextOrder;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;break;&lt;/code&gt; out of the &lt;code&gt;while (true)&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Moving to the next item&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;num++;&lt;/code&gt; followed by &lt;code&gt;continue;&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Inner loop finishing normally&lt;/td&gt;
&lt;td&gt;The bounds check fails, then "Order shipped!", then &lt;code&gt;break;&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The key is what comes &lt;em&gt;after&lt;/em&gt; the &lt;code&gt;while (true)&lt;/code&gt; loop: nothing. The outer &lt;code&gt;foreach&lt;/code&gt; body ends right there. So breaking out of the inner loop drops you straight into the next iteration of the outer loop, which is exactly what &lt;code&gt;continue nextOrder;&lt;/code&gt; did.&lt;/p&gt;

&lt;p&gt;The "Order shipped!" line moved inside the loop so it only runs on the "all items packed" path. The out-of-stock path breaks out before reaching it.&lt;/p&gt;

&lt;p&gt;ILSpy took a labeled jump across two loops and turned it into a plain &lt;code&gt;break&lt;/code&gt; out of a single loop. It's clever, and it's definitely not code a human would write.&lt;/p&gt;




&lt;h2&gt;
  
  
  Bonus: Two Compiler Tricks Hiding in Plain Sight
&lt;/h2&gt;

&lt;p&gt;While we're here, the decompiled code reveals two things the C# compiler does with every &lt;code&gt;foreach&lt;/code&gt; over an array, labeled or not:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;There's no enumerator.&lt;/strong&gt; A &lt;code&gt;foreach&lt;/code&gt; over an array doesn't call &lt;code&gt;GetEnumerator()&lt;/code&gt;. The compiler turns it into a plain index-based loop, because that's faster. That's where &lt;code&gt;num&lt;/code&gt; and &lt;code&gt;array3.Length&lt;/code&gt; come from.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hidden temporary variables.&lt;/strong&gt; &lt;code&gt;array2 = array&lt;/code&gt; and &lt;code&gt;array3 = obj&lt;/code&gt; are extra locals the compiler introduces for &lt;code&gt;foreach&lt;/code&gt; over arrays. They copy the reference, not the array itself. You never wrote them, but they're part of how the loop is lowered.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of this is new in C# 15, but it's a nice reminder of how much the compiler rewrites behind the scenes, even for the simplest loops.&lt;/p&gt;




&lt;h2&gt;
  
  
  So What Does This Mean?
&lt;/h2&gt;

&lt;p&gt;Let's put it all together.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The label is a compile-time thing only.&lt;/strong&gt; &lt;code&gt;nextOrder:&lt;/code&gt; helps &lt;em&gt;you&lt;/em&gt; say which loop you mean, and it helps the compiler check that you're only jumping to a loop you're actually inside. Once the compiler has done that check, the label has nothing left to do, so it doesn't make it into the IL at all.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There's no runtime cost.&lt;/strong&gt; The entire &lt;code&gt;continue nextOrder;&lt;/code&gt; becomes a single jump instruction. The old &lt;code&gt;bool&lt;/code&gt; flag approach would need an extra local variable, an assignment, and an extra check after the inner loop. The labeled version needs one jump, and the JIT just sees a branch like any other.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;And at the IL level, everything is a jump.&lt;/strong&gt; In the previous article, I asked whether labeled loops are just &lt;code&gt;goto&lt;/code&gt; in a nicer suit. After popping the hood, the honest answer is that in IL, &lt;em&gt;everything&lt;/em&gt; is &lt;code&gt;goto&lt;/code&gt;. Your &lt;code&gt;for&lt;/code&gt; loops, your &lt;code&gt;while&lt;/code&gt; loops, your &lt;code&gt;if&lt;/code&gt; statements, your &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt;: they all end up as &lt;code&gt;br&lt;/code&gt;, &lt;code&gt;brtrue&lt;/code&gt;, &lt;code&gt;brfalse&lt;/code&gt;, and &lt;code&gt;blt&lt;/code&gt;. The difference between &lt;code&gt;goto&lt;/code&gt; and a labeled &lt;code&gt;continue&lt;/code&gt; isn't in the IL. It's in what the &lt;em&gt;compiler&lt;/em&gt; lets you do before it gets there.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;goto&lt;/code&gt; lets you jump almost anywhere. A labeled &lt;code&gt;continue&lt;/code&gt; only lets you jump to the next iteration of a loop you're inside. Same instruction at the end, very different rules on the way in.&lt;/p&gt;




&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;Here's what we found under the hood:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The decompiled C# doesn't look like the original.&lt;/strong&gt; ILSpy rebuilt our inner loop as a &lt;code&gt;while (true)&lt;/code&gt; and turned &lt;code&gt;continue nextOrder;&lt;/code&gt; into a plain &lt;code&gt;break&lt;/code&gt;, because decompiled code is a reconstruction, not our source.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;continue nextOrder;&lt;/code&gt; compiles to a single jump instruction.&lt;/strong&gt; IL has no loops or labels, only branches, so the whole feature boils down to one jump to the right place.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Labels don't exist at runtime.&lt;/strong&gt; They're a compile-time tool for readability and safety, with zero runtime cost.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, the feature named after labels doesn't have a single label left after compilation. Kind of poetic, isn't it?&lt;/p&gt;

&lt;p&gt;All the code from this article is in my .NET 11 playground: &lt;a href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;dotnet11-playground&lt;/a&gt;. Clone it, open it in ILSpy, and see for yourself.&lt;/p&gt;

&lt;p&gt;Have you ever opened your own code in a decompiler and barely recognized it? Tell me about it in the comments. 👇&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Let's stay connected!&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;💼 &lt;a href="https://www.linkedin.com/in/giorgikobaidze/" rel="noopener noreferrer"&gt;Connect with me on LinkedIn&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💻 &lt;a href="https://github.com/georgekobaidze" rel="noopener noreferrer"&gt;Explore my projects on GitHub&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📱 &lt;a href="https://x.com/georgekobaidze" rel="noopener noreferrer"&gt;Follow me on X&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🎥 &lt;a href="https://www.youtube.com/@Pilotronica" rel="noopener noreferrer"&gt;Watch my videos on YouTube&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;a href="https://discord.com/users/571315867426488330" rel="noopener noreferrer"&gt;Let's Chat on Discord&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>microsoft</category>
      <category>dotnet</category>
      <category>csharp</category>
      <category>programming</category>
    </item>
    <item>
      <title>C# 15 Unpacked: The Retro New Feature Nobody Expected in 2026</title>
      <dc:creator>Giorgi Kobaidze</dc:creator>
      <pubDate>Tue, 24 Nov 2026 19:01:44 +0000</pubDate>
      <link>https://dev.to/georgekobaidze/c-15-unpacked-the-retro-new-feature-nobody-expected-in-2026-38a</link>
      <guid>https://dev.to/georgekobaidze/c-15-unpacked-the-retro-new-feature-nobody-expected-in-2026-38a</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;All the code in this series is available on GitHub. Clone it, run it, and try breaking it yourself.&lt;/strong&gt; &lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://assets.dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/georgekobaidze" rel="noopener noreferrer"&gt;
        georgekobaidze
      &lt;/a&gt; / &lt;a href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;
        dotnet11-playground
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      A lightweight playground repository for experimenting with and testing new features introduced in .NET 11 and C# 15.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;.NET 11 &amp;amp; C# 15 Playground&lt;/h1&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer nofollow" href="https://raw.githubusercontent.com/georgekobaidze/dotnet11-playground/main/docs/cover.jpg"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fgeorgekobaidze%2Fdotnet11-playground%2Fmain%2Fdocs%2Fcover.jpg" alt=".NET 11 &amp;amp; C# 15 Playground"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;A lightweight playground repository for experimenting with and testing new features introduced in &lt;strong&gt;.NET 11&lt;/strong&gt; and &lt;strong&gt;C# 15&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Use it to explore language changes, try out APIs, and validate ideas in isolation without production constraints. Each feature lives in its own small project, often with a "before" and "after" version so you can see what the new feature actually changes.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;What's inside&lt;/h2&gt;
&lt;/div&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Project&lt;/th&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/georgekobaidze/dotnet11-playground/tree/main/src/Unions" rel="noopener noreferrer"&gt;Unions&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Union types&lt;/td&gt;
&lt;td&gt;Console examples: the login flow (before and after unions), plus samples for parsing input, a state machine, user commands, and combining types you don't own.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/georgekobaidze/dotnet11-playground/tree/main/src/UnionsWebApi" rel="noopener noreferrer"&gt;UnionsWebApi&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Union types&lt;/td&gt;
&lt;td&gt;A minimal API that returns a union from a service and maps each outcome to an HTTP response.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;More examples will be added as new features are explored.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Getting started&lt;/h2&gt;
&lt;/div&gt;
&lt;p&gt;You'll need the &lt;a href="https://dotnet.microsoft.com/download/dotnet/11.0" rel="nofollow noopener noreferrer"&gt;.NET 11 SDK&lt;/a&gt;. Then clone the repo and run any example:&lt;/p&gt;
&lt;div class="highlight highlight-source-shell notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;dotnet run&lt;/pre&gt;…
&lt;/div&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Follow me on GitHub for more updates!&lt;/p&gt;




&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Floppy Disk Vibes in 2026&lt;/li&gt;
&lt;li&gt;24 Years Late to the Party&lt;/li&gt;
&lt;li&gt;The Nested Loop Tax&lt;/li&gt;
&lt;li&gt;Wait, C# Already Had Labels?&lt;/li&gt;
&lt;li&gt;Enter C# 15&lt;/li&gt;
&lt;li&gt;Labeled break vs goto&lt;/li&gt;
&lt;li&gt;So... How Do I Feel About It?&lt;/li&gt;
&lt;li&gt;Meet Labeled continue&lt;/li&gt;
&lt;li&gt;Escaping a Loop From Inside a switch&lt;/li&gt;
&lt;li&gt;The Rules of the Game&lt;/li&gt;
&lt;li&gt;Your IDE Already Wants You to Use It&lt;/li&gt;
&lt;li&gt;Wrapping Up&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Floppy Disk Vibes in 2026
&lt;/h2&gt;

&lt;p&gt;C# 15 is here, and one of its new features has such an old-school vibe, it could come with a floppy disk.&lt;/p&gt;

&lt;p&gt;Yes, I'm talking about labeled &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt;, also known as labeled loops.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Wait, what's a floppy disk?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you don't know what a floppy disk is, congratulations, you're probably still in your 20s. Here's what they look like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9azz4c1hd6ggua18mwul.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9azz4c1hd6ggua18mwul.jpeg" alt="Floppy Disks" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And yes, those are mine. They still work.&lt;/p&gt;

&lt;p&gt;Though I'm not &lt;em&gt;that&lt;/em&gt; old. I just stepped into my 30s, which makes me a proud millennial. Old enough to remember floppy disks, young enough that my knees only complain after leg day.&lt;/p&gt;

&lt;p&gt;Holding working floppy disks in 2026 feels exactly like using labeled loops in C# 15. Both are technology from another era, both still do their job perfectly, and both make you wonder: &lt;em&gt;"Wait, why does this feel new?"&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  24 Years Late to the Party
&lt;/h2&gt;

&lt;p&gt;This feature is about 24 years late to the party. C# 1.0 shipped in 2002, and this feels like something that should have been in the box on day one.&lt;/p&gt;

&lt;p&gt;Here's why it feels so late. C# was heavily inspired by Java (with a good dose of C++), and Java has had labeled loops since its very first release in 1996. Thirty years ago. In Java, breaking out of a nested loop has always looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Found it: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;" x "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt; &lt;span class="n"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// exits BOTH loops&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One label, one &lt;code&gt;break outer;&lt;/code&gt;, and you're out of both loops. No flags, no tricks, no drama. Easy.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Nested Loop Tax
&lt;/h2&gt;

&lt;p&gt;Meanwhile, C# developers spent more than two decades writing this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;found&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"Found it: {i} x {j}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;found&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// only exits the inner loop...&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;found&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ...so we need a second break for the outer one&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you've written C# for more than a week, you've written this. We all have. It's the nested loop tax.&lt;/p&gt;




&lt;h2&gt;
  
  
  Wait, C# Already Had Labels?
&lt;/h2&gt;

&lt;p&gt;Here's something even weirder: C# has had labels since day one. They just had exactly one job: being a target for &lt;code&gt;goto&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For anyone who hasn't seen &lt;code&gt;goto&lt;/code&gt; in the wild (lucky you), here's how it works in C#:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"Found it: {i} x {j}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;goto&lt;/span&gt; &lt;span class="n"&gt;done&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// jump straight to the label below&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Out of the loops!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You put a label in the method (&lt;code&gt;done:&lt;/code&gt;), and &lt;code&gt;goto done;&lt;/code&gt; teleports execution there. It works, and it solves the nested loop problem, but &lt;code&gt;goto&lt;/code&gt; has a reputation... a bad one. A really, really bad one. Don't use it. Just don't.&lt;/p&gt;

&lt;p&gt;The problem with &lt;code&gt;goto&lt;/code&gt; is that it can jump almost anywhere in the method. The label can be above you, below you, or 200 lines away, so you have to go hunting for it to understand what happens next. Your code can get messy really fast.&lt;/p&gt;




&lt;h2&gt;
  
  
  Enter C# 15
&lt;/h2&gt;

&lt;p&gt;C# 15 takes those same labels and lets &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt; use them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"Found it: {i} x {j}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt; &lt;span class="n"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// exits BOTH loops&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look familiar? It's almost character-for-character the Java version from earlier.&lt;/p&gt;




&lt;h2&gt;
  
  
  Labeled &lt;code&gt;break&lt;/code&gt; vs &lt;code&gt;goto&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The key difference from &lt;code&gt;goto&lt;/code&gt; is that the label sits on the loop itself, and &lt;code&gt;break outer;&lt;/code&gt; can only target a loop (or &lt;code&gt;switch&lt;/code&gt;) that you're currently inside. You can't jump to random places or into the middle of something else. It's controlled, structured, and the intent is obvious just from reading the line: "get me out of the &lt;code&gt;outer&lt;/code&gt; loop."&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;bool&lt;/code&gt; flag&lt;/th&gt;
&lt;th&gt;&lt;code&gt;goto&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Labeled &lt;code&gt;break&lt;/code&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Extra state to track&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can jump anywhere&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Almost 😬&lt;/td&gt;
&lt;td&gt;No, only to enclosing loops or &lt;code&gt;switch&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Intent readable at the jump&lt;/td&gt;
&lt;td&gt;Meh&lt;/td&gt;
&lt;td&gt;Not really&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Gets you side-eye in code review&lt;/td&gt;
&lt;td&gt;Sometimes&lt;/td&gt;
&lt;td&gt;Always&lt;/td&gt;
&lt;td&gt;We'll see...&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  So... How Do I Feel About It?
&lt;/h2&gt;

&lt;p&gt;To be honest, I don't know yet.&lt;/p&gt;

&lt;p&gt;Part of me thinks it's weird. Labels have always meant "&lt;code&gt;goto&lt;/code&gt; lives here, be careful," and that reflex is hard to unlearn.&lt;/p&gt;

&lt;p&gt;But another part of me kinda likes it. It makes the code simpler. The flag variable is gone, the second &lt;code&gt;break&lt;/code&gt; is gone, and the logic reads exactly the way I'd explain it out loud.&lt;/p&gt;

&lt;p&gt;The truth is, I haven't used it enough in real-world code to make up my mind (duh... it's a brand-new feature). So in this article, I'm going to do what I do with every new feature: poke it, push it, and see where it shines and where it starts to smell.&lt;/p&gt;

&lt;p&gt;Let's unpack it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Meet Labeled &lt;code&gt;continue&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;So far, we've only talked about &lt;code&gt;break&lt;/code&gt;. But &lt;code&gt;continue&lt;/code&gt; got the same upgrade, and it might be the more useful of the two.&lt;/p&gt;

&lt;p&gt;A regular &lt;code&gt;continue&lt;/code&gt; skips to the next iteration of the &lt;em&gt;innermost&lt;/em&gt; loop. A labeled &lt;code&gt;continue&lt;/code&gt; skips to the next iteration of &lt;em&gt;whichever&lt;/em&gt; loop you name.&lt;/p&gt;

&lt;p&gt;Let's say we're packing orders, and if any item in an order is out of stock, we want to skip the entire order and move on to the next one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Keyboard"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Mouse"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Monitor"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"OUT_OF_STOCK"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Cable"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Headphones"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;nextOrder&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"New order:"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"OUT_OF_STOCK"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"  Item missing, skipping this order."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt; &lt;span class="n"&gt;nextOrder&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// jump to the next ORDER, not the next item&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"  Packing {item}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"  Order shipped!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fl3gjf4dv11vf7mqwzixj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fl3gjf4dv11vf7mqwzixj.png" alt="Console output of the labeled continue example" width="752" height="492"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice that the second order never prints "Order shipped!". The &lt;code&gt;continue nextOrder;&lt;/code&gt; skipped the rest of the outer loop body and went straight to the next order.&lt;/p&gt;

&lt;p&gt;Also notice the label name. I could've called it &lt;code&gt;outer&lt;/code&gt;, but &lt;code&gt;nextOrder&lt;/code&gt; makes &lt;code&gt;continue nextOrder;&lt;/code&gt; read like plain English. &lt;strong&gt;Name your labels by intent, not by position.&lt;/strong&gt; Future you will thank you.&lt;/p&gt;

&lt;h3&gt;
  
  
  How We Did It Before C# 15
&lt;/h3&gt;

&lt;p&gt;Here's the same logic without labeled loops, the way we've been writing it for years:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"New order:"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;skipOrder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"OUT_OF_STOCK"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"  Item missing, skipping this order."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;skipOrder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// step 1: get out of the inner loop...&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"  Packing {item}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;skipOrder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// step 2: ...then skip the rest of the outer loop&lt;/span&gt;

    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"  Order shipped!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works, but look at what it costs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;An extra variable&lt;/strong&gt; whose only job is to carry a message from the inner loop to the outer one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Two jumps instead of one:&lt;/strong&gt; a &lt;code&gt;break&lt;/code&gt; inside, then a &lt;code&gt;continue&lt;/code&gt; outside.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A hidden trap:&lt;/strong&gt; &lt;code&gt;skipOrder&lt;/code&gt; has to be declared &lt;em&gt;inside&lt;/em&gt; the outer loop so it resets for every order. Move it above the outer &lt;code&gt;foreach&lt;/code&gt;, and after the first skipped order, every order after it gets skipped too. It still compiles, there's no warning, and the output is quietly wrong.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With C# 15, all of that collapses into a single line: &lt;code&gt;continue nextOrder;&lt;/code&gt;. That's really what a labeled &lt;code&gt;continue&lt;/code&gt; is: a &lt;code&gt;break&lt;/code&gt; from the inner loop and a &lt;code&gt;continue&lt;/code&gt; on the outer loop, in one statement.&lt;/p&gt;




&lt;h2&gt;
  
  
  Escaping a Loop From Inside a &lt;code&gt;switch&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This one has annoyed C# developers forever. Inside a &lt;code&gt;switch&lt;/code&gt;, &lt;code&gt;break&lt;/code&gt; belongs to the &lt;code&gt;switch&lt;/code&gt;, not the loop around it. So if you want to exit the loop from within a &lt;code&gt;case&lt;/code&gt;, a plain &lt;code&gt;break&lt;/code&gt; won't help.&lt;/p&gt;

&lt;p&gt;Labeled &lt;code&gt;break&lt;/code&gt; fixes that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;commands&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"move"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"jump"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"quit"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"move"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;gameLoop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;command&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;commands&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"move"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Moving..."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// exits the switch only&lt;/span&gt;

        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"jump"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Jumping..."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// exits the switch only&lt;/span&gt;

        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"quit"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Quitting the game."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt; &lt;span class="n"&gt;gameLoop&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// exits the whole loop&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F13ak72zszwhy1eg7x72n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F13ak72zszwhy1eg7x72n.png" alt="Console output of the labeled break in switch example" width="764" height="262"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The last &lt;code&gt;"move"&lt;/code&gt; never runs, because &lt;code&gt;break gameLoop;&lt;/code&gt; jumped out of both the &lt;code&gt;switch&lt;/code&gt; and the &lt;code&gt;foreach&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Rules of the Game
&lt;/h2&gt;

&lt;p&gt;Labeled jumps are simple, but there are a few rules worth knowing before you start sprinkling labels everywhere.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rule #1: &lt;code&gt;break&lt;/code&gt; can target loops and &lt;code&gt;switch&lt;/code&gt;. &lt;code&gt;continue&lt;/code&gt; can only target loops.
&lt;/h3&gt;

&lt;p&gt;That makes sense when you think about it. You can &lt;em&gt;exit&lt;/em&gt; a &lt;code&gt;switch&lt;/code&gt;, but there's no "next iteration" of a &lt;code&gt;switch&lt;/code&gt; to continue to. So this won't compile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;mySwitch&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;continue&lt;/span&gt; &lt;span class="n"&gt;mySwitch&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ❌ compile error: continue needs a loop&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Rule #2: You can only jump to something you're inside.
&lt;/h3&gt;

&lt;p&gt;Labeled &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt; only work with an &lt;em&gt;enclosing&lt;/em&gt; loop or &lt;code&gt;switch&lt;/code&gt;. You can't use them to jump sideways into another loop or to some random spot in the method. This is exactly what makes them safer than &lt;code&gt;goto&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rule #3: Watch out for stacked labels.
&lt;/h3&gt;

&lt;p&gt;This is the sneaky one. C# technically lets you put more than one label in front of a statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ❌ compile error&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might expect both labels to point to the &lt;code&gt;for&lt;/code&gt; loop, but they don't. Only the label &lt;em&gt;directly&lt;/em&gt; in front of the loop (&lt;code&gt;second&lt;/code&gt;) labels the loop. &lt;code&gt;first&lt;/code&gt; labels the &lt;code&gt;second: for (...)&lt;/code&gt; labeled statement, which isn't a loop. So &lt;code&gt;break first;&lt;/code&gt; fails to compile, while &lt;code&gt;break second;&lt;/code&gt; works just fine.&lt;/p&gt;

&lt;p&gt;Is this something you'll run into every day?&lt;br&gt;
No.&lt;/p&gt;

&lt;p&gt;Will it confuse someone on your team at 6 PM on a Friday?&lt;br&gt;
Absolutely.&lt;/p&gt;
&lt;h3&gt;
  
  
  Rule #4: Without a label, nothing changes.
&lt;/h3&gt;

&lt;p&gt;Plain &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt; work exactly as they always did. This feature is purely additive, so none of your existing code changes behavior.&lt;/p&gt;


&lt;h2&gt;
  
  
  Your IDE Already Wants You to Use It
&lt;/h2&gt;

&lt;p&gt;Here's a fun surprise. .NET 11 comes with a new code style rule, &lt;strong&gt;IDE0410 ("Use labeled jump statement")&lt;/strong&gt;, that looks for the old patterns: &lt;code&gt;bool&lt;/code&gt; flags propagating a break outward, &lt;code&gt;goto&lt;/code&gt; jumping past nested loops, and &lt;code&gt;goto&lt;/code&gt; to an empty label at the end of a loop body. It then suggests replacing them with a labeled &lt;code&gt;break&lt;/code&gt; or &lt;code&gt;continue&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvdb0raiusysa8q2xeeoc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvdb0raiusysa8q2xeeoc.png" alt="Editor Suggestion" width="800" height="683"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's on by default for C# 15 projects. So if you open an old codebase full of &lt;code&gt;found = true; break;&lt;/code&gt; patterns after upgrading, don't be surprised when your IDE lights up like a Christmas tree.&lt;/p&gt;

&lt;p&gt;If you're not ready for that, you can turn it off in your &lt;code&gt;.editorconfig&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="k"&gt;[*.cs]&lt;/span&gt;
&lt;span class="na"&gt;dotnet_diagnostic.IDE0410.severity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;none&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;Labeled &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt; aren't flashy. They won't make headlines like union types did. But they fix a small, annoying problem that C# developers have been working around for 24 years.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick cheat sheet:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use a labeled &lt;code&gt;break&lt;/code&gt; to exit an outer loop, or a loop from inside a &lt;code&gt;switch&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Use a labeled &lt;code&gt;continue&lt;/code&gt; to skip to the next iteration of an outer loop.&lt;/li&gt;
&lt;li&gt;Name labels by intent (&lt;code&gt;nextOrder&lt;/code&gt;, &lt;code&gt;gameLoop&lt;/code&gt;), not position (&lt;code&gt;outer&lt;/code&gt;, &lt;code&gt;loop1&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Avoid stacked labels.&lt;/li&gt;
&lt;li&gt;Keep it simple: one label per loop nest is usually plenty.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, do I like it? I think I do. It's like my floppy disks: retro, a little weird in 2026, but it still does its job perfectly.&lt;/p&gt;

&lt;p&gt;All the code from this article is in my .NET 11 playground: &lt;a href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;dotnet11-playground&lt;/a&gt;. Feel free to clone it, break it, and contribute.&lt;/p&gt;

&lt;p&gt;What about you? Are you going to use labeled loops, or do they give you &lt;code&gt;goto&lt;/code&gt; flashbacks? Let me know in the comments. 👇&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Let's stay connected!&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;💼 &lt;a href="https://www.linkedin.com/in/giorgikobaidze/" rel="noopener noreferrer"&gt;Connect with me on LinkedIn&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💻 &lt;a href="https://github.com/georgekobaidze" rel="noopener noreferrer"&gt;Explore my projects on GitHub&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📱 &lt;a href="https://x.com/georgekobaidze" rel="noopener noreferrer"&gt;Follow me on X&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🎥 &lt;a href="https://www.youtube.com/@Pilotronica" rel="noopener noreferrer"&gt;Watch my videos on YouTube&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;a href="https://discord.com/users/571315867426488330" rel="noopener noreferrer"&gt;Let's Chat on Discord&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>microsoft</category>
      <category>dotnet</category>
      <category>csharp</category>
      <category>programming</category>
    </item>
    <item>
      <title>C# 15 Unpacked: Practical Use Cases for Unions with Real Examples</title>
      <dc:creator>Giorgi Kobaidze</dc:creator>
      <pubDate>Mon, 23 Nov 2026 13:24:24 +0000</pubDate>
      <link>https://dev.to/georgekobaidze/c-15-unpacked-practical-use-cases-for-unions-with-real-examples-e32</link>
      <guid>https://dev.to/georgekobaidze/c-15-unpacked-practical-use-cases-for-unions-with-real-examples-e32</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;All the code in this series is available on GitHub. Clone it, run it, and try breaking it yourself.&lt;/strong&gt; &lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://assets.dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/georgekobaidze" rel="noopener noreferrer"&gt;
        georgekobaidze
      &lt;/a&gt; / &lt;a href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;
        dotnet11-playground
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      A lightweight playground repository for experimenting with and testing new features introduced in .NET 11 and C# 15.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;.NET 11 &amp;amp; C# 15 Playground&lt;/h1&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer nofollow" href="https://raw.githubusercontent.com/georgekobaidze/dotnet11-playground/main/docs/cover.jpg"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fgeorgekobaidze%2Fdotnet11-playground%2Fmain%2Fdocs%2Fcover.jpg" alt=".NET 11 &amp;amp; C# 15 Playground"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;A lightweight playground repository for experimenting with and testing new features introduced in &lt;strong&gt;.NET 11&lt;/strong&gt; and &lt;strong&gt;C# 15&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Use it to explore language changes, try out APIs, and validate ideas in isolation without production constraints. Each feature lives in its own small project, often with a "before" and "after" version so you can see what the new feature actually changes.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;What's inside&lt;/h2&gt;
&lt;/div&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Project&lt;/th&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/georgekobaidze/dotnet11-playground/tree/main/src/Unions" rel="noopener noreferrer"&gt;Unions&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Union types&lt;/td&gt;
&lt;td&gt;Console examples: the login flow (before and after unions), plus samples for parsing input, a state machine, user commands, and combining types you don't own.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/georgekobaidze/dotnet11-playground/tree/main/src/UnionsWebApi" rel="noopener noreferrer"&gt;UnionsWebApi&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Union types&lt;/td&gt;
&lt;td&gt;A minimal API that returns a union from a service and maps each outcome to an HTTP response.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;More examples will be added as new features are explored.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Getting started&lt;/h2&gt;
&lt;/div&gt;
&lt;p&gt;You'll need the &lt;a href="https://dotnet.microsoft.com/download/dotnet/11.0" rel="nofollow noopener noreferrer"&gt;.NET 11 SDK&lt;/a&gt;. Then clone the repo and run any example:&lt;/p&gt;
&lt;div class="highlight highlight-source-shell notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;dotnet run&lt;/pre&gt;…
&lt;/div&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Follow me on GitHub for more updates!&lt;/p&gt;




&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;One Last Round with Unions&lt;/li&gt;
&lt;li&gt;1. Parsing and Validating Input&lt;/li&gt;
&lt;li&gt;2. Returning Results from an API&lt;/li&gt;
&lt;li&gt;3. Modeling a State Machine&lt;/li&gt;
&lt;li&gt;4. Handling Commands from User Input&lt;/li&gt;
&lt;li&gt;5. Combining Types You Don't Own&lt;/li&gt;
&lt;li&gt;When Not to Use Unions&lt;/li&gt;
&lt;li&gt;Wrapping Up&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  One Last Round with Unions
&lt;/h2&gt;

&lt;p&gt;The last two articles in this series were all about unions. In the &lt;a href="https://dev.to/georgekobaidze/c-15-unpacked-unions-have-arrived-heres-why-they-matter-48je"&gt;first one&lt;/a&gt;, we looked at what unions are and whether we actually need them. In the &lt;a href="https://dev.to/georgekobaidze/c-15-unpacked-popping-the-hood-on-unions-and-finding-a-struct-59f0"&gt;second one&lt;/a&gt;, we popped the hood and found a struct.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.NET 11 has a lot more to offer, so I promise this is the last union article in the series.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And this time, it's all about practice. We're software engineers. We don't really trust a new feature until we've used it to solve a real problem. That's where its value shows up.&lt;/p&gt;

&lt;p&gt;So no theory and no IL this time. Just five common problems, the workarounds we've all written for them, and how unions solve each one better:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Parsing and validating input&lt;/li&gt;
&lt;li&gt;Returning results from an API&lt;/li&gt;
&lt;li&gt;Modeling a state machine&lt;/li&gt;
&lt;li&gt;Handling commands from user input&lt;/li&gt;
&lt;li&gt;Combining types you don't own&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Every example is runnable, and all the code is in the &lt;a href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The four console examples live in &lt;code&gt;src/Unions&lt;/code&gt; (uncomment the one you want in &lt;code&gt;Program.cs&lt;/code&gt;), and the API example is a separate project in &lt;code&gt;src/UnionsWebApi&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet run --project src/Unions
dotnet run --project src/UnionsWebApi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  1. Parsing and Validating Input
&lt;/h2&gt;

&lt;p&gt;Every app takes input it can't trust: console input, query strings, config files, CSV rows. Parsing can go wrong in more than one way, and the caller usually needs to know &lt;em&gt;which&lt;/em&gt; way.&lt;/p&gt;

&lt;p&gt;Let's parse a user's age. It has to be a number, and it has to be realistic.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Old Way
&lt;/h3&gt;

&lt;p&gt;The classic &lt;code&gt;TryParse&lt;/code&gt; pattern tells you &lt;em&gt;that&lt;/em&gt; something failed, but not &lt;em&gt;why&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nc"&gt;TryParseAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TryParse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Was it "abc"? Was it "-5"? The caller gets &lt;code&gt;false&lt;/code&gt; and has to guess, so the user sees a vague "Invalid age."&lt;/p&gt;

&lt;p&gt;The other classic approach is to throw:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nc"&gt;ParseAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TryParse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FormatException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"'{input}' is not a number."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArgumentOutOfRangeException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;nameof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; $&lt;span class="s2"&gt;"{age} is not a realistic age."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have the reason, but we're using exceptions for something that happens all the time. Users mistype things constantly. Nothing in the signature warns the caller to catch them, either. &lt;code&gt;int ParseAge(string)&lt;/code&gt; looks like it always succeeds.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Union Way
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;ParseError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;union&lt;/span&gt; &lt;span class="nc"&gt;ParseResult&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ParseError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AgeParser&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;ParseResult&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TryParse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ParseError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"'{input}' is not a number."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ParseError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"{age} is not a realistic age."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And a small console loop to try it out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Enter your age: "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// the input stream was closed (Ctrl+Z on Windows, Ctrl+D on macOS/Linux)&lt;/span&gt;

    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;AgeParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"Got it, you're {age}."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;ParseError&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"Oops: {error.Message}"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Type &lt;code&gt;34&lt;/code&gt;, then &lt;code&gt;abc&lt;/code&gt;, then &lt;code&gt;200&lt;/code&gt;, and you get a clear answer for each.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why It's Better
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The reason travels with the result.&lt;/strong&gt; Instead of a bare &lt;code&gt;false&lt;/code&gt;, the caller gets a &lt;code&gt;ParseError&lt;/code&gt; with a message it can show the user.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The signature tells the truth.&lt;/strong&gt; &lt;code&gt;ParseResult&amp;lt;int&amp;gt;&lt;/code&gt; says "you'll get an &lt;code&gt;int&lt;/code&gt; or an error" right in the method signature. Nobody has to read the implementation to find out what can go wrong.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You can't forget the failure case.&lt;/strong&gt; To get the &lt;code&gt;int&lt;/code&gt; out, you have to match on the union, and the compiler makes sure the &lt;code&gt;ParseError&lt;/code&gt; arm is there.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No exceptions for normal input.&lt;/strong&gt; Bad input is expected, not exceptional. Exceptions stay reserved for real bugs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It's reusable.&lt;/strong&gt; &lt;code&gt;ParseResult&amp;lt;T&amp;gt;&lt;/code&gt; is generic, so the same union works for &lt;code&gt;ParseResult&amp;lt;DateOnly&amp;gt;&lt;/code&gt;, &lt;code&gt;ParseResult&amp;lt;Uri&amp;gt;&lt;/code&gt;, or anything else you parse.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2. Returning Results from an API
&lt;/h2&gt;

&lt;p&gt;Most API endpoints have more than one outcome. Looking up a user, for example: the user exists, the user doesn't exist, or the user exists but you're not allowed to see them.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Old Way
&lt;/h3&gt;

&lt;p&gt;Usually, each outcome gets a different mechanism. "Not found" becomes &lt;code&gt;null&lt;/code&gt;. "Forbidden" becomes an exception:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;GetUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;_users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TryGetValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Id&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nc"&gt;AdminId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;UnauthorizedAccessException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Admin profiles are private."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the endpoint has to deal with both:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MapGet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/users/{id}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;Results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NotFound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;UnauthorizedAccessException&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Problem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;StatusCodes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Status403Forbidden&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The signature &lt;code&gt;User? GetUser(int id)&lt;/code&gt; only tells half the story. You can guess that &lt;code&gt;null&lt;/code&gt; means "not found", but nothing tells you about the exception. Forget the &lt;code&gt;try/catch&lt;/code&gt;, and a perfectly normal "you can't see this" turns into a 500 error.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Union Way
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nc"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;UserNotFound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nc"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;AccessDenied&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Reason&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;union&lt;/span&gt; &lt;span class="nc"&gt;GetUserResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;UserNotFound&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;AccessDenied&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nc"&gt;AdminId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nc"&gt;Dictionary&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Ada"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Linus"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;AdminId&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AdminId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Admin"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;GetUserResult&lt;/span&gt; &lt;span class="nc"&gt;GetUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;_users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TryGetValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;UserNotFound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Id&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nc"&gt;AdminId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AccessDenied&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Admin profiles are private."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the endpoint maps each outcome to an HTTP response in one place:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;WebApplication&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CreateBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AddSingleton&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;UserService&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MapGet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/users/{id}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;UserNotFound&lt;/span&gt; &lt;span class="n"&gt;notFound&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NotFound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"User {notFound.Id} doesn't exist."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;AccessDenied&lt;/span&gt; &lt;span class="n"&gt;denied&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Problem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;denied&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Reason&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;StatusCodes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Status403Forbidden&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it and try all three outcomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -i http://localhost:5115/users/1    &lt;span class="c"&gt;# 200 OK&lt;/span&gt;
curl -i http://localhost:5115/users/99   &lt;span class="c"&gt;# 404 Not Found&lt;/span&gt;
curl -i http://localhost:5115/users/100  &lt;span class="c"&gt;# 403 Forbidden&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Port &lt;code&gt;5115&lt;/code&gt; is what the app uses on my machine. Your port may be different, especially if you created your own project. Use the URL printed in the console when the app starts.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Why It's Better
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Every outcome is in the signature.&lt;/strong&gt; &lt;code&gt;GetUserResult&lt;/code&gt; tells you there are exactly three possibilities, and what each one carries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One mechanism instead of two.&lt;/strong&gt; No mix of &lt;code&gt;null&lt;/code&gt; and exceptions. Every outcome is just a value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The service doesn't know about HTTP.&lt;/strong&gt; &lt;code&gt;UserService&lt;/code&gt; returns domain results, not &lt;code&gt;IResult&lt;/code&gt; or status codes. The endpoint decides how each one becomes a response, so the same service works for a gRPC call, a background job, or a test.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;New outcomes can't slip through.&lt;/strong&gt; Add a &lt;code&gt;UserSuspended&lt;/code&gt; case later, and the compiler flags the endpoint until you decide what HTTP status it gets.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Why not &lt;code&gt;Results.Forbid()&lt;/code&gt;?&lt;/strong&gt; It looks like the obvious choice for a 403, but it hands the request to the authentication system. If your app has no authentication configured, it throws at runtime. &lt;code&gt;Results.Problem(..., statusCode: 403)&lt;/code&gt; works everywhere.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  3. Modeling a State Machine
&lt;/h2&gt;

&lt;p&gt;Lots of things in software move through states: a network connection, an order, a payment, a background job. Each state usually comes with its own data. A connection that's &lt;em&gt;connecting&lt;/em&gt; knows which attempt it's on. A &lt;em&gt;connected&lt;/em&gt; one knows when it connected. A &lt;em&gt;failed&lt;/em&gt; one knows why.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Old Way
&lt;/h3&gt;

&lt;p&gt;The usual approach is an enum for the state, plus a property for every piece of data any state might need:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="nc"&gt;ConnectionStatus&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Disconnected&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Connecting&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Connected&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Failed&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Connection&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ConnectionStatus&lt;/span&gt; &lt;span class="nc"&gt;Status&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;Attempt&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;                    &lt;span class="c1"&gt;// only when Connecting&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;DateTimeOffset&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;ConnectedSince&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;  &lt;span class="c1"&gt;// only when Connected&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;FailureReason&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;           &lt;span class="c1"&gt;// only when Failed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Those comments are doing a lot of work. Nothing stops &lt;code&gt;Status&lt;/code&gt; from being &lt;code&gt;Connected&lt;/code&gt; while &lt;code&gt;ConnectedSince&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;FailureReason&lt;/code&gt; is set. Every reader has to remember which fields are valid in which state, and every property is nullable, so the code fills up with &lt;code&gt;?&lt;/code&gt; and &lt;code&gt;!&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Even the &lt;code&gt;switch&lt;/code&gt; can't be fully trusted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Connection&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Status&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;ConnectionStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Disconnected&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Offline"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;ConnectionStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Connecting&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"Connecting (attempt {c.Attempt})..."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;ConnectionStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Connected&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"Online since {c.ConnectedSince:T}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;ConnectionStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Failed&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"Failed: {c.FailureReason}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Unknown"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An enum is just a number underneath, so &lt;code&gt;(ConnectionStatus)42&lt;/code&gt; is a valid value. The compiler warns unless you add a &lt;code&gt;_&lt;/code&gt; arm, and that arm swallows any new state you add later.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Union Way
&lt;/h3&gt;

&lt;p&gt;Each state becomes a record that carries only its own data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Disconnected&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Connecting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nc"&gt;Attempt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Connected&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DateTimeOffset&lt;/span&gt; &lt;span class="nc"&gt;Since&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Failed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Reason&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;union&lt;/span&gt; &lt;span class="nc"&gt;ConnectionState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Disconnected&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Connecting&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Connected&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Failed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The transitions live in one place:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ConnectionMachine&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nc"&gt;MaxAttempts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;ConnectionState&lt;/span&gt; &lt;span class="nc"&gt;Next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ConnectionState&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;serverResponds&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Disconnected&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Connecting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;Connecting&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="n"&gt;serverResponds&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Connected&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DateTimeOffset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;Connecting&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Attempt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nc"&gt;MaxAttempts&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Connecting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Attempt&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;Connecting&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Failed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"No response after {MaxAttempts} attempts."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;Connected&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="n"&gt;serverResponds&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;Connected&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Disconnected&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;Failed&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Disconnected&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ConnectionState&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Disconnected&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Offline"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;Connecting&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"Connecting (attempt {c.Attempt})..."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;Connected&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"Online since {c.Since:T}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;Failed&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"Failed: {f.Reason}"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And a small loop lets you play the server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;ConnectionState&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Disconnected&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"State: {ConnectionMachine.Describe(state)}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Does the server respond? (y/n): "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// the input stream was closed (Ctrl+Z on Windows, Ctrl+D on macOS/Linux)&lt;/span&gt;

    &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ConnectionMachine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;serverResponds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ToLower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"y"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Answer &lt;code&gt;n&lt;/code&gt; three times while connecting and watch it fail. Answer &lt;code&gt;y&lt;/code&gt; and it connects. Answer &lt;code&gt;n&lt;/code&gt; while connected and it drops back to offline.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why It's Better
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Impossible states can't exist.&lt;/strong&gt; There's no way to build a &lt;code&gt;Connected&lt;/code&gt; without a &lt;code&gt;Since&lt;/code&gt; date, or a &lt;code&gt;Failed&lt;/code&gt; without a reason. The data and the state are the same thing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No nullable fields.&lt;/strong&gt; &lt;code&gt;Connected.Since&lt;/code&gt; is a plain &lt;code&gt;DateTimeOffset&lt;/code&gt;. You never check for &lt;code&gt;null&lt;/code&gt; because the value can't be missing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;All transitions in one exhaustive &lt;code&gt;switch&lt;/code&gt;.&lt;/strong&gt; Reading &lt;code&gt;Next&lt;/code&gt; tells you the whole state machine at a glance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;New states can't be forgotten.&lt;/strong&gt; Add a &lt;code&gt;Reconnecting&lt;/code&gt; state, and the compiler flags both &lt;code&gt;Next&lt;/code&gt; and &lt;code&gt;Describe&lt;/code&gt; until you handle it. No &lt;code&gt;_&lt;/code&gt; arm to hide it.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4. Handling Commands from User Input
&lt;/h2&gt;

&lt;p&gt;Command-line tools, chat bots and game consoles all do the same thing: read a line of text, figure out which command it is, and run it. The parsing and the running tend to get tangled together.&lt;/p&gt;

&lt;p&gt;Let's build a tiny to-do list with four commands: &lt;code&gt;add&lt;/code&gt;, &lt;code&gt;done&lt;/code&gt;, &lt;code&gt;list&lt;/code&gt; and &lt;code&gt;quit&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Old Way
&lt;/h3&gt;

&lt;p&gt;The usual approach is one big &lt;code&gt;switch&lt;/code&gt; on strings, with parsing, validation and execution all mixed together:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"add"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"What should I add?"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"done"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TryParse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Which item?"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;RemoveAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"list"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;// print the items...&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"quit"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Unknown command."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works for four commands. At fifteen, it's a wall of code. The command logic can't be tested without the console, &lt;code&gt;"add"&lt;/code&gt; is just a string that's easy to mistype, and adding a new command means digging through the whole block.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Union Way
&lt;/h3&gt;

&lt;p&gt;First, each command becomes a record that carries exactly the data it needs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Done&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;ShowList&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Quit&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Invalid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Reason&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;union&lt;/span&gt; &lt;span class="nc"&gt;Command&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Done&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ShowList&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Quit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Invalid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then parsing happens in one place, and only there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CommandParser&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Command&lt;/span&gt; &lt;span class="nc"&gt;Parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;StringSplitOptions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;RemoveEmptyEntries&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Invalid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Type a command: add, done, list or quit."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;argument&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ToLowerInvariant&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="s2"&gt;"add"&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="n"&gt;argument&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;argument&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;"add"&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Invalid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"What should I add? Try: add Buy milk"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;"done"&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TryParse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;argument&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Done&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;"done"&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Invalid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Which item? Try: done 1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;"list"&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ShowList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;"quit"&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Quit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Invalid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"Unknown command '{parts[0]}'."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And running a command never deals with strings again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TodoList&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Command&lt;/span&gt; &lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;command&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Add&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;AddItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;Done&lt;/span&gt; &lt;span class="n"&gt;done&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Complete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;done&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;ShowList&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Show&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;Quit&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Bye!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;Invalid&lt;/span&gt; &lt;span class="n"&gt;invalid&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;invalid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Reason&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;AddItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; $&lt;span class="s2"&gt;"Added: {text}"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Complete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; $&lt;span class="s2"&gt;"There's no item {number}."&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;_items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;RemoveAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; $&lt;span class="s2"&gt;"Done: {item}"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Show&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;_items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Count&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
            &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;"Nothing to do. Nice!"&lt;/span&gt;
            &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NewLine&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"{i + 1}. {item}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, the loop that ties it together:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;todo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TodoList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Commands: add &amp;lt;text&amp;gt;, done &amp;lt;number&amp;gt;, list, quit"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&amp;gt; "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// the input stream was closed (Ctrl+Z on Windows, Ctrl+D on macOS/Linux)&lt;/span&gt;

    &lt;span class="nc"&gt;Command&lt;/span&gt; &lt;span class="n"&gt;command&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CommandParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;command&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Quit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try &lt;code&gt;add Buy milk&lt;/code&gt;, &lt;code&gt;add Write the next article&lt;/code&gt;, &lt;code&gt;list&lt;/code&gt;, &lt;code&gt;done 1&lt;/code&gt;, and &lt;code&gt;list&lt;/code&gt; again.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why It's Better
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Parse once, then work with types.&lt;/strong&gt; Text only exists at the edge, in &lt;code&gt;CommandParser&lt;/code&gt;. Everything after that works with real types. &lt;code&gt;Done&lt;/code&gt; carries an &lt;code&gt;int&lt;/code&gt;, not a string that might be a number.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Each piece has one job.&lt;/strong&gt; The parser turns text into commands, and &lt;code&gt;TodoList&lt;/code&gt; runs them. You can test the parser without a to-do list, and the to-do list without a console.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The &lt;code&gt;_&lt;/code&gt; arm stays where it belongs.&lt;/strong&gt; Strings can be anything, so the parser needs a fallback. But &lt;code&gt;Execute&lt;/code&gt; matches on the union, so it needs no &lt;code&gt;_&lt;/code&gt; arm and stays fully checked.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;New commands can't be half-added.&lt;/strong&gt; Add a &lt;code&gt;Rename&lt;/code&gt; record to the union, and the compiler flags &lt;code&gt;Execute&lt;/code&gt; until you handle it. You can't forget the second half of the work.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  5. Combining Types You Don't Own
&lt;/h2&gt;

&lt;p&gt;Sometimes a value can be one of several types you didn't write and can't change. Think of IDs coming from different systems: an old database uses &lt;code&gt;int&lt;/code&gt;, the CRM uses &lt;code&gt;string&lt;/code&gt; codes, and the new cloud service uses &lt;code&gt;Guid&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Old Way
&lt;/h3&gt;

&lt;p&gt;Option one is to give up on types and use &lt;code&gt;object&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;ToKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"legacy-{number}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"crm-{code}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Guid&lt;/span&gt; &lt;span class="n"&gt;guid&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"cloud-{guid:N}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArgumentException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"Unsupported ID type: {id.GetType().Name}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The signature accepts &lt;em&gt;anything&lt;/em&gt;. &lt;code&gt;ToKey(3.14)&lt;/code&gt; and &lt;code&gt;ToKey(DateTime.Now)&lt;/code&gt; both compile, and you only find out at runtime.&lt;/p&gt;

&lt;p&gt;Option two is to wrap every type in your own hierarchy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;ExternalId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;LegacyId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nc"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ExternalId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;CrmId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ExternalId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;CloudId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Guid&lt;/span&gt; &lt;span class="nc"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ExternalId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's type-safe, but now you need three wrapper classes for values that were perfectly fine on their own, and every caller has to write &lt;code&gt;new LegacyId(1042)&lt;/code&gt; instead of just &lt;code&gt;1042&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Union Way
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;union&lt;/span&gt; &lt;span class="nc"&gt;ExternalId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Guid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it. No wrappers, no base class. &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;string&lt;/code&gt; and &lt;code&gt;Guid&lt;/code&gt; are case types as they are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;IdMapper&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;ExternalId&lt;/span&gt; &lt;span class="nc"&gt;Parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TryParse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Guid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TryParse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="nc"&gt;Guid&lt;/span&gt; &lt;span class="n"&gt;guid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;guid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;ToKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ExternalId&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"legacy-{number}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"crm-{code}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;Guid&lt;/span&gt; &lt;span class="n"&gt;guid&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"cloud-{guid:N}"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A small loop to try it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Enter an ID: "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// the input stream was closed (Ctrl+Z on Windows, Ctrl+D on macOS/Linux)&lt;/span&gt;

    &lt;span class="nc"&gt;ExternalId&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;IdMapper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"Key: {IdMapper.ToKey(id)}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try &lt;code&gt;1042&lt;/code&gt;, &lt;code&gt;ACME-77&lt;/code&gt;, and a GUID like &lt;code&gt;3f2504e0-4f89-11d3-9a0c-0305e82c3301&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Compare that to the &lt;code&gt;object&lt;/code&gt; version. There, &lt;code&gt;ToKey(3.14)&lt;/code&gt; compiles fine and only crashes at runtime, when the &lt;code&gt;_ =&amp;gt; throw&lt;/code&gt; arm fires. With the union, the same mistake doesn't even compile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;IdMapper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ToKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1042&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// ✅ an int: compiles&lt;/span&gt;
&lt;span class="nc"&gt;IdMapper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ToKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ACME-77"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// ✅ a string: compiles&lt;/span&gt;
&lt;span class="nc"&gt;IdMapper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ToKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.14&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// ❌ a double: compile error, double isn't one of the case types&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why It's Better
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No wrapper classes.&lt;/strong&gt; Types you don't own can be case types directly. No class hierarchy can do that, because you can't make &lt;code&gt;int&lt;/code&gt; inherit from your base class.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Type-safe, unlike &lt;code&gt;object&lt;/code&gt;.&lt;/strong&gt; The signature says "an &lt;code&gt;int&lt;/code&gt;, a &lt;code&gt;string&lt;/code&gt; or a &lt;code&gt;Guid&lt;/code&gt;", and the compiler enforces it at every call site.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exhaustive, unlike &lt;code&gt;object&lt;/code&gt;.&lt;/strong&gt; No &lt;code&gt;_ =&amp;gt; throw&lt;/code&gt; arm. Add a &lt;code&gt;long&lt;/code&gt; case later, and the compiler shows you every &lt;code&gt;switch&lt;/code&gt; to update.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  One Thing to Keep in Mind: Boxing
&lt;/h3&gt;

&lt;p&gt;As we saw in the &lt;a href="https://dev.to/georgekobaidze/c-15-unpacked-popping-the-hood-on-unions-and-finding-a-struct-59f0"&gt;previous article&lt;/a&gt;, a union stores its value in an &lt;code&gt;object? Value&lt;/code&gt; property. &lt;code&gt;int&lt;/code&gt; and &lt;code&gt;Guid&lt;/code&gt; are value types, so every time an &lt;code&gt;ExternalId&lt;/code&gt; holds one, the value gets &lt;strong&gt;boxed&lt;/strong&gt;: copied into a small object on the heap.&lt;/p&gt;

&lt;p&gt;For mapping IDs, parsing input, or handling API results, that cost doesn't matter. In a hot loop that processes millions of values, it might. In that case, you can write your own union struct with the &lt;code&gt;[Union]&lt;/code&gt; attribute and add &lt;code&gt;HasValue&lt;/code&gt; and &lt;code&gt;TryGetValue&lt;/code&gt; members. The compiler then uses those for pattern matching and skips the boxing. For everyday code, the plain &lt;code&gt;union&lt;/code&gt; keyword is the right choice.&lt;/p&gt;




&lt;h2&gt;
  
  
  When Not to Use Unions
&lt;/h2&gt;

&lt;p&gt;Unions are a great tool, but not for everything. A few cases where something else fits better:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;When the failure is a bug.&lt;/strong&gt; A &lt;code&gt;null&lt;/code&gt; argument that should never be &lt;code&gt;null&lt;/code&gt;, or a broken invariant, is exceptional. Throw an exception. Unions are for outcomes the caller is &lt;em&gt;expected&lt;/em&gt; to handle, like bad input or a missing user, not for things that should never happen.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When the cases carry no data.&lt;/strong&gt; If you only need "red, yellow or green" with nothing attached, a plain &lt;code&gt;enum&lt;/code&gt; is simpler. Unions start paying off when each case carries different data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When the cases share behavior.&lt;/strong&gt; If every case needs the same method, like &lt;code&gt;Area()&lt;/code&gt; for different shapes, a class hierarchy with a virtual method (or a &lt;code&gt;closed&lt;/code&gt; hierarchy) is often cleaner. If you find yourself writing the same &lt;code&gt;switch&lt;/code&gt; in five places just to call "the same thing" on each case, that's a sign.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In public library APIs that will grow.&lt;/strong&gt; Adding a case to a public union breaks every consumer's exhaustive &lt;code&gt;switch&lt;/code&gt;. That's great inside your own codebase, where the compiler shows you what to fix. For a library used by people you don't know, it's a breaking change.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In hot paths with value-type cases.&lt;/strong&gt; Remember the boxing from use case 5. Measure first. If it matters, use a custom &lt;code&gt;[Union]&lt;/code&gt; struct or a different design.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And one rule for wherever you do use them: &lt;strong&gt;dispatch with switch expressions.&lt;/strong&gt; Classic &lt;code&gt;switch&lt;/code&gt; statements and &lt;code&gt;if&lt;/code&gt;/&lt;code&gt;else&lt;/code&gt; chains don't get exhaustiveness checking, so the safety net only works if you use it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;Five problems, five workarounds we've all written, and five unions that replaced them:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Problem&lt;/th&gt;
&lt;th&gt;The old workaround&lt;/th&gt;
&lt;th&gt;The union&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Parsing input&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;bool TryParse(..., out T)&lt;/code&gt; or exceptions&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ParseResult&amp;lt;T&amp;gt;(T, ParseError)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;API results&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;null&lt;/code&gt; for one outcome, exceptions for another&lt;/td&gt;
&lt;td&gt;&lt;code&gt;GetUserResult(User, UserNotFound, AccessDenied)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;State machines&lt;/td&gt;
&lt;td&gt;An enum plus nullable fields&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ConnectionState(Disconnected, Connecting, Connected, Failed)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;User commands&lt;/td&gt;
&lt;td&gt;A big &lt;code&gt;switch&lt;/code&gt; on strings&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Command(Add, Done, ShowList, Quit, Invalid)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Types you don't own&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;object&lt;/code&gt; or wrapper classes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ExternalId(int, string, Guid)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In every case, the pattern is the same: a union turns "what can happen here?" from a comment, a convention or a guess into something the compiler knows and checks for you.&lt;/p&gt;

&lt;p&gt;That wraps up unions in this series. We've seen what they are, what they compile to, and how to put them to work. .NET 11 has plenty more to explore, so stay tuned.&lt;/p&gt;

&lt;p&gt;All the code from this article is in the &lt;a href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;. Clone it, run the examples, and try adding a new case to each union. Watching the compiler point out everything you forgot is half the fun.&lt;/p&gt;

&lt;p&gt;Which of these five would you use first in your own code? Let me know in the comments.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Let's stay connected!&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;💼 &lt;a href="https://www.linkedin.com/in/giorgikobaidze/" rel="noopener noreferrer"&gt;Connect with me on LinkedIn&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💻 &lt;a href="https://github.com/georgekobaidze" rel="noopener noreferrer"&gt;Explore my projects on GitHub&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📱 &lt;a href="https://x.com/georgekobaidze" rel="noopener noreferrer"&gt;Follow me on X&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🎥 &lt;a href="https://www.youtube.com/@Pilotronica" rel="noopener noreferrer"&gt;Watch my videos on YouTube&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;a href="https://discord.com/users/571315867426488330" rel="noopener noreferrer"&gt;Let's Chat on Discord&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>microsoft</category>
      <category>dotnet</category>
      <category>csharp</category>
      <category>programming</category>
    </item>
    <item>
      <title>C# 15 Unpacked: Popping the Hood on Unions and Finding a Struct</title>
      <dc:creator>Giorgi Kobaidze</dc:creator>
      <pubDate>Sat, 21 Nov 2026 20:27:37 +0000</pubDate>
      <link>https://dev.to/georgekobaidze/c-15-unpacked-popping-the-hood-on-unions-and-finding-a-struct-59f0</link>
      <guid>https://dev.to/georgekobaidze/c-15-unpacked-popping-the-hood-on-unions-and-finding-a-struct-59f0</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;All the code in this series is available on GitHub. Clone it, run it, and try breaking it yourself.&lt;/strong&gt; &lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://assets.dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/georgekobaidze" rel="noopener noreferrer"&gt;
        georgekobaidze
      &lt;/a&gt; / &lt;a href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;
        dotnet11-playground
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      A lightweight playground repository for experimenting with and testing new features introduced in .NET 11 and C# 15.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;.NET 11 &amp;amp; C# 15 Playground&lt;/h1&gt;

&lt;/div&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer" href="https://github.com/georgekobaidze/dotnet11-playground/docs/cover.jpg"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fgeorgekobaidze%2Fdotnet11-playground%2FHEAD%2Fdocs%2Fcover.jpg" alt=".NET 11 &amp;amp; C# 15 Playground"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;A lightweight playground repository for experimenting with and testing new features introduced in &lt;strong&gt;.NET 11&lt;/strong&gt; and &lt;strong&gt;C# 15&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Use it to explore language changes, try out APIs, and validate ideas in isolation without production constraints. Each feature lives in its own small project, often with a "before" and "after" version so you can see what the new feature actually changes.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;What's inside&lt;/h2&gt;

&lt;/div&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Project&lt;/th&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/georgekobaidze/dotnet11-playground/src/Unions" rel="noopener noreferrer"&gt;Unions&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Union types&lt;/td&gt;
&lt;td&gt;A small console login flow that compares the classic class-hierarchy approach with C# 15 unions and compiler-checked exhaustive matching.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;More examples will be added as new features are explored.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Getting started&lt;/h2&gt;

&lt;/div&gt;
&lt;p&gt;You'll need the &lt;a href="https://dotnet.microsoft.com/download/dotnet/11.0" rel="nofollow noopener noreferrer"&gt;.NET 11 SDK&lt;/a&gt;. Then clone the repo and run any example:&lt;/p&gt;
&lt;div class="highlight highlight-source-shell notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;dotnet run --project src/Unions&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Disclaimer&lt;/h2&gt;

&lt;/div&gt;
&lt;p&gt;.NET 11 and C# 15 features may still change. The code here is for learning and experimentation, not production use.&lt;/p&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;p&gt;Follow me on GitHub for more updates!&lt;/p&gt;




&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Unions Aren't Magic&lt;/li&gt;
&lt;li&gt;What You'll Need&lt;/li&gt;
&lt;li&gt;
Opening the DLL

&lt;ul&gt;
&lt;li&gt;Decompiling with ILSpy&lt;/li&gt;
&lt;li&gt;The First Surprise&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
Breaking Down the Struct

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;[Union]&lt;/code&gt;: A Label for the Compiler&lt;/li&gt;
&lt;li&gt;&lt;code&gt;struct&lt;/code&gt;: A Value Type&lt;/li&gt;
&lt;li&gt;&lt;code&gt;IUnion&lt;/code&gt; and &lt;code&gt;Value&lt;/code&gt;: One Slot for Every Case&lt;/li&gt;
&lt;li&gt;One Constructor per Case&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Where the Conversions Happen&lt;/li&gt;
&lt;li&gt;What a Switch on a Union Becomes&lt;/li&gt;
&lt;li&gt;Wrapping Up&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Unions Aren't Magic
&lt;/h2&gt;

&lt;p&gt;Unions are great. You declare a type in one line and the compiler checks every &lt;code&gt;switch&lt;/code&gt; expression for you, and the "impossible" branch finally stays impossible. Joy of joys!&lt;/p&gt;

&lt;p&gt;But unions aren't magic. There's no new runtime type, no special instruction in the CLR, and no hidden framework doing the work at runtime. Everything happens at compile time. The compiler takes your one-line &lt;code&gt;union&lt;/code&gt; declaration and turns it into ordinary C# that .NET has understood for years.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So what does that ordinary code look like?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the &lt;a href="https://dev.to/georgekobaidze/c-15-unpacked-unions-have-arrived-heres-why-they-matter-48je"&gt;previous article&lt;/a&gt; of this series, we looked at what unions are, how to use them, and whether we actually need them. This time, we're going to pop the hood. We'll take the project from the previous article, open its compiled DLL, and look at what the compiler really generates when you write &lt;code&gt;union&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Major spoiler alert: it's a struct.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  What You'll Need
&lt;/h2&gt;

&lt;p&gt;You don't need much to follow along:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;.NET 11 SDK.&lt;/strong&gt; Run &lt;code&gt;dotnet --list-sdks&lt;/code&gt; and check that you see &lt;code&gt;11.0.x&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;VS Code&lt;/strong&gt; with the &lt;strong&gt;C# Dev Kit&lt;/strong&gt; extension, or any other .NET IDE.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A way to see IL.&lt;/strong&gt; In VS Code, install the &lt;strong&gt;ILSpy .NET Decompiler&lt;/strong&gt; extension (&lt;code&gt;icsharpcode.ilspy-vscode&lt;/code&gt;). ILSpy also runs as a standalone app. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmtrgq67xr3hf8fggfvsu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmtrgq67xr3hf8fggfvsu.png" alt="ILSpy" width="799" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you use &lt;strong&gt;JetBrains Rider&lt;/strong&gt;, you don't need anything extra: it has a built-in &lt;strong&gt;IL Viewer&lt;/strong&gt; that shows the IL for the code under your cursor, and it can also decompile assemblies.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4ac9tyolrb94ccthchiw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4ac9tyolrb94ccthchiw.png" alt="Rider" width="768" height="1302"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The project from the previous article.&lt;/strong&gt; Clone the repository and build it in Release mode:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/georgekobaidze/dotnet11-playground.git
&lt;span class="nb"&gt;cd&lt;/span&gt; dotnet11-playground
dotnet build -c Release
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why Release? Debug builds add extra instructions and variables to make debugging easier. Release shows you what actually ships.&lt;/p&gt;

&lt;p&gt;After the build, the compiled DLL is at &lt;code&gt;src/Unions/bin/Release/net11.0/Unions.dll&lt;/code&gt;. That's the file we're going to open.&lt;/p&gt;




&lt;h2&gt;
  
  
  Opening the DLL
&lt;/h2&gt;

&lt;p&gt;Let's start with the union from the previous article. Here's its source code, from &lt;code&gt;src/Unions/LoginService/LoginAfter.cs&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;LoggedIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;UserName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;InvalidCredentials&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nc"&gt;AttemptsLeft&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;AccountLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DateTimeOffset&lt;/span&gt; &lt;span class="nc"&gt;Until&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;TwoFactorRequired&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Method&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;union&lt;/span&gt; &lt;span class="nc"&gt;LoginResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LoggedIn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;InvalidCredentials&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;AccountLocked&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;TwoFactorRequired&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Four records and one line that groups them. Now let's see what that line turned into.&lt;/p&gt;

&lt;h3&gt;
  
  
  Decompiling with ILSpy
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Open the command palette (&lt;code&gt;Ctrl+Shift+P&lt;/code&gt;, or &lt;code&gt;Cmd+Shift+P&lt;/code&gt; on macOS) and type &lt;code&gt;ILSpy&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Choose the command that opens an assembly, and pick &lt;strong&gt;&lt;code&gt;src/Unions/bin/Release/net11.0/Unions.dll&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;A new &lt;strong&gt;ILSpy: Assemblies&lt;/strong&gt; view appears in the Explorer. Expand &lt;code&gt;Unions&lt;/code&gt;, then the &lt;code&gt;Unions.After&lt;/code&gt; namespace, and find &lt;code&gt;LoginResult&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Click it. ILSpy decompiles it and shows the result in an editor tab.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By default, ILSpy shows decompiled C#. To see the raw IL, click the &lt;strong&gt;Output: C# …&lt;/strong&gt; item in VS Code's status bar (bottom right) and pick &lt;strong&gt;IL&lt;/strong&gt;. We'll use both: C# to understand the shape of the type, and IL to see exactly what the compiler emitted.&lt;/p&gt;

&lt;h3&gt;
  
  
  The First Surprise
&lt;/h3&gt;

&lt;p&gt;The project has two types called &lt;code&gt;LoginResult&lt;/code&gt;, one from each version of the login code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Unions.Before.LoginResult&lt;/code&gt; is the abstract record we used before C# 15.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Unions.After.LoginResult&lt;/code&gt; is the union.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First let's look at the decompiled C# code of the old &lt;code&gt;LoginResult&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fpfpvbsj7en8vjh0mr6wb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fpfpvbsj7en8vjh0mr6wb.png" alt="Old LoginResult" width="800" height="674"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's an abstract record, just like in the source code.&lt;/p&gt;

&lt;p&gt;Now let's look at the new &lt;code&gt;LoginResult&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzfci1iozuhc4tuexf8yj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzfci1iozuhc4tuexf8yj.png" alt="New LoginResult" width="800" height="591"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see here, it's a struct behind the mask. Unions, you've been exposed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8may0tgdi0m8oor505na.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8may0tgdi0m8oor505na.jpg" alt="Meme" width="500" height="667"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's the main idea of this article: there's no "union" in the compiled code. The runtime has never heard of unions. There's only an ordinary struct that the compiler wrote for you.&lt;/p&gt;

&lt;p&gt;And now let's look at the IL code.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A quick warning about the IL view: don't try to read it line by line. IL is a low-level, stack-based instruction set written for the runtime, not for humans. Skim it for the parts we care about (the type declaration, its attributes, and the method signatures) and leave the rest to the JIT compiler.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fooiy4r3uvdzj5tdvxh45.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fooiy4r3uvdzj5tdvxh45.png" alt="LoginResult IL code" width="800" height="909"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's break this struct down piece by piece and see what each part does.&lt;/p&gt;




&lt;h2&gt;
  
  
  Breaking Down the Struct
&lt;/h2&gt;

&lt;p&gt;Let's go through the generated &lt;code&gt;LoginResult&lt;/code&gt; from top to bottom.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;[Union]&lt;/code&gt;: A Label for the Compiler
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Union&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;LoginResult&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;IUnion&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first thing on the type is the &lt;code&gt;[Union]&lt;/code&gt; attribute. It comes from &lt;code&gt;System.Runtime.CompilerServices&lt;/code&gt;, which is why ILSpy added that &lt;code&gt;using&lt;/code&gt; at the top of the file.&lt;/p&gt;

&lt;p&gt;The attribute doesn't do anything at runtime. It's a label for the &lt;strong&gt;compiler&lt;/strong&gt;. When another project references &lt;code&gt;Unions.dll&lt;/code&gt; and uses &lt;code&gt;LoginResult&lt;/code&gt;, the compiler reads this attribute and knows to treat the struct as a union: it allows the implicit conversions and checks your &lt;code&gt;switch&lt;/code&gt; expressions for exhaustiveness.&lt;/p&gt;

&lt;p&gt;It also means the &lt;code&gt;union&lt;/code&gt; keyword isn't the only way to get a union. Any struct or class with the &lt;code&gt;[Union]&lt;/code&gt; attribute and the right shape (single-parameter constructors and a &lt;code&gt;Value&lt;/code&gt; property) gets the same treatment. Union libraries can use this to plug into the compiler's checks without users switching to the new syntax.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;struct&lt;/code&gt;: A Value Type
&lt;/h3&gt;

&lt;p&gt;The compiler chose a &lt;strong&gt;struct&lt;/strong&gt;, not a class. That means a &lt;code&gt;LoginResult&lt;/code&gt; variable holds the union directly, not a reference to it, and creating one doesn't allocate a separate wrapper object on the heap. The wrapper is just one field, as we'll see in a moment.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;IUnion&lt;/code&gt; and &lt;code&gt;Value&lt;/code&gt;: One Slot for Every Case
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the heart of the union. There's &lt;strong&gt;one&lt;/strong&gt; property, and it holds whichever case is current. Look at the assembly tree: there's only one field, &lt;code&gt;&amp;lt;Value&amp;gt;k__BackingField&lt;/code&gt;, the compiler-generated storage behind this auto-property. That's why a union can never hold two cases at once: &lt;strong&gt;there's simply nowhere to put the second one.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Notice the type: &lt;code&gt;object?&lt;/code&gt;. At runtime, the struct doesn't know or care that &lt;code&gt;Value&lt;/code&gt; can only be a &lt;code&gt;LoggedIn&lt;/code&gt;, &lt;code&gt;InvalidCredentials&lt;/code&gt;, &lt;code&gt;AccountLocked&lt;/code&gt; or &lt;code&gt;TwoFactorRequired&lt;/code&gt;. That rule exists only at compile time. The compiler enforces it, and the generated code just stores an &lt;code&gt;object&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The property is also get-only, so a union can't change after it's created. The IL backs this up: the backing field is marked &lt;code&gt;initonly&lt;/code&gt; (IL's version of &lt;code&gt;readonly&lt;/code&gt;), and the getter carries an &lt;code&gt;IsReadOnly&lt;/code&gt; attribute. When you assign a new case to a variable, you replace the whole struct.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;IUnion&lt;/code&gt; interface is the contract behind this property. Any union, whether declared with &lt;code&gt;union&lt;/code&gt; or hand-written with &lt;code&gt;[Union]&lt;/code&gt;, exposes its current case through &lt;code&gt;Value&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  One Constructor per Case
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;CompilerGenerated&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;LoginResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LoggedIn&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The compiler generated one constructor for each case type: &lt;code&gt;LoggedIn&lt;/code&gt;, &lt;code&gt;InvalidCredentials&lt;/code&gt;, &lt;code&gt;AccountLocked&lt;/code&gt;, and &lt;code&gt;TwoFactorRequired&lt;/code&gt;. Each one does exactly one thing: it stores the value in &lt;code&gt;Value&lt;/code&gt;. The &lt;code&gt;[CompilerGenerated]&lt;/code&gt; attribute marks them as code you didn't write yourself.&lt;/p&gt;

&lt;p&gt;These constructors are where the implicit conversions come from. We'll see them in action in a moment.&lt;/p&gt;

&lt;p&gt;It also explains why only the listed types are accepted. There's no &lt;code&gt;LoginResult(string)&lt;/code&gt; constructor, so there's no conversion from &lt;code&gt;string&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Where the Conversions Happen
&lt;/h2&gt;

&lt;p&gt;Remember this line from &lt;code&gt;AuthService.Login&lt;/code&gt;?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InvalidCredentials&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MaxAttempts&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;failed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The method returns a &lt;code&gt;LoginResult&lt;/code&gt;, but we return an &lt;code&gt;InvalidCredentials&lt;/code&gt;. Open &lt;code&gt;Unions.After&lt;/code&gt; → &lt;code&gt;AuthService&lt;/code&gt; → &lt;code&gt;Login(string, string)&lt;/code&gt; in ILSpy to see how that works:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4ieplpko3rwxd3hkuvot.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4ieplpko3rwxd3hkuvot.png" alt="Login decompiled C#" width="799" height="409"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There's no magic conversion. The compiler simply wrapped the record in one of the constructors we saw earlier, the one that takes an &lt;code&gt;InvalidCredentials&lt;/code&gt;. That's why we didn't have to change a single line of &lt;code&gt;AuthService&lt;/code&gt; in the previous article. (And if you're wondering where &lt;code&gt;MaxAttempts&lt;/code&gt; went: it's a &lt;code&gt;const&lt;/code&gt;, so the compiler replaced it with its value, &lt;code&gt;3&lt;/code&gt;.) In the IL view, you'll see it as a &lt;code&gt;newobj&lt;/code&gt; instruction calling &lt;code&gt;LoginResult::.ctor&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9tl6563r6xc99ixjuj19.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9tl6563r6xc99ixjuj19.png" alt="Login IL code" width="800" height="497"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  What a Switch on a Union Becomes
&lt;/h2&gt;

&lt;p&gt;Now for the part that makes unions worth using: the exhaustive &lt;code&gt;switch&lt;/code&gt;. Our top-level code in &lt;code&gt;Program.cs&lt;/code&gt; compiles into a hidden &lt;code&gt;Program&lt;/code&gt; class with a &lt;code&gt;&amp;lt;Main&amp;gt;$&lt;/code&gt; method. You'll find it under the empty namespace node (&lt;code&gt;{ }&lt;/code&gt;) in the assembly tree.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhlnxregffqhkp5ywbzit.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhlnxregffqhkp5ywbzit.png" alt="Switch expression" width="800" height="867"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The compiler copies &lt;code&gt;loginResult.Value&lt;/code&gt; into a local variable, and the &lt;code&gt;switch&lt;/code&gt; expression becomes a series of &lt;strong&gt;type checks on &lt;code&gt;Value&lt;/code&gt;&lt;/strong&gt;: is it a &lt;code&gt;LoggedIn&lt;/code&gt;? An &lt;code&gt;InvalidCredentials&lt;/code&gt;? An &lt;code&gt;AccountLocked&lt;/code&gt;? A &lt;code&gt;TwoFactorRequired&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Look closely: our exhaustive &lt;code&gt;switch&lt;/code&gt; expression turned into a chain of nested &lt;code&gt;if&lt;/code&gt;/&lt;code&gt;else&lt;/code&gt; checks. That's the exact form that &lt;em&gt;doesn't&lt;/em&gt; get exhaustiveness checking when you write it yourself. The safety comes from what you write, not from what runs.&lt;/p&gt;

&lt;p&gt;So where's the exhaustiveness? It's not in the output. It was checked &lt;strong&gt;at compile time&lt;/strong&gt; and then thrown away. The runtime never sees it, which is why the compiler still adds one last branch: if none of the type checks match, it calls a compiler-generated helper that throws a &lt;code&gt;SwitchExpressionException&lt;/code&gt;. That branch runs only in one situation: an empty, &lt;code&gt;default&lt;/code&gt; union whose &lt;code&gt;Value&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The compiler checks your work. The runtime just runs plain type checks.&lt;/p&gt;




&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;We popped the hood, and here's what we found: unions aren't magic. They're a compiler feature, not a runtime feature.&lt;/p&gt;

&lt;p&gt;When you write one line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;union&lt;/span&gt; &lt;span class="nc"&gt;LoginResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LoggedIn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;InvalidCredentials&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;AccountLocked&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;TwoFactorRequired&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the compiler turns it into an ordinary struct:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a &lt;code&gt;[Union]&lt;/code&gt; attribute and the &lt;code&gt;IUnion&lt;/code&gt; interface, so the compiler recognizes it as a union,&lt;/li&gt;
&lt;li&gt;a single &lt;code&gt;object? Value&lt;/code&gt; property that holds the current case,&lt;/li&gt;
&lt;li&gt;and one constructor per case type, which is where the implicit conversions come from.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Your &lt;code&gt;switch&lt;/code&gt; expressions become plain type checks on &lt;code&gt;Value&lt;/code&gt;. The exhaustiveness check, the feature that makes unions worth using, happens entirely at compile time. By the time your code runs, it's gone.&lt;/p&gt;

&lt;p&gt;Knowing this explains a few things that might otherwise feel strange:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Why &lt;code&gt;default&lt;/code&gt; is empty.&lt;/strong&gt; It's a struct, and every struct has a default value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why value-type cases get boxed.&lt;/strong&gt; Everything is stored in an &lt;code&gt;object?&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why switch expressions matter.&lt;/strong&gt; The compiler can only check what it sees at compile time. Write the same logic as an &lt;code&gt;if/else&lt;/code&gt; chain or a classic &lt;code&gt;switch&lt;/code&gt; statement, and that check is gone.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of this makes unions less useful. It makes them predictable. You now know exactly what you're shipping.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I have to admit: in the AI era, there's something incredibly satisfying about digging through low-level IL code yourself.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you haven't read it yet, start with the &lt;a href="https://dev.to/georgekobaidze/c-15-unpacked-unions-have-arrived-heres-why-they-matter-48je"&gt;first article&lt;/a&gt; to see unions in action. All the code from both articles is in the &lt;a href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;. Clone it, open &lt;code&gt;Unions.dll&lt;/code&gt; in ILSpy or Rider, and pop the hood yourself.&lt;/p&gt;

&lt;p&gt;What surprised you most about what the compiler generates? Let me know in the comments.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Let's stay connected!&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;💼 &lt;a href="https://www.linkedin.com/in/giorgikobaidze/" rel="noopener noreferrer"&gt;Connect with me on LinkedIn&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💻 &lt;a href="https://github.com/georgekobaidze" rel="noopener noreferrer"&gt;Explore my projects on GitHub&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📱 &lt;a href="https://x.com/georgekobaidze" rel="noopener noreferrer"&gt;Follow me on X&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🎥 &lt;a href="https://www.youtube.com/@Pilotronica" rel="noopener noreferrer"&gt;Watch my videos on YouTube&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;a href="https://discord.com/users/571315867426488330" rel="noopener noreferrer"&gt;Let's Chat on Discord&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>microsoft</category>
      <category>programming</category>
    </item>
    <item>
      <title>C# 15 Unpacked: Unions Have Arrived. Here's Why They Matter</title>
      <dc:creator>Giorgi Kobaidze</dc:creator>
      <pubDate>Thu, 19 Nov 2026 21:20:16 +0000</pubDate>
      <link>https://dev.to/georgekobaidze/c-15-unpacked-unions-have-arrived-heres-why-they-matter-48je</link>
      <guid>https://dev.to/georgekobaidze/c-15-unpacked-unions-have-arrived-heres-why-they-matter-48je</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;All the code in this series is available on GitHub. Clone it, run it, and try breaking it yourself.&lt;/strong&gt; &lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://assets.dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/georgekobaidze" rel="noopener noreferrer"&gt;
        georgekobaidze
      &lt;/a&gt; / &lt;a href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;
        dotnet11-playground
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      A lightweight playground repository for experimenting with and testing new features introduced in .NET 11 and C# 15.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;.NET 11 &amp;amp; C# 15 Playground&lt;/h1&gt;

&lt;/div&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer" href="https://github.com/georgekobaidze/dotnet11-playground/docs/cover.jpg"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fgeorgekobaidze%2Fdotnet11-playground%2FHEAD%2Fdocs%2Fcover.jpg" alt=".NET 11 &amp;amp; C# 15 Playground"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;A lightweight playground repository for experimenting with and testing new features introduced in &lt;strong&gt;.NET 11&lt;/strong&gt; and &lt;strong&gt;C# 15&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Use it to explore language changes, try out APIs, and validate ideas in isolation without production constraints. Each feature lives in its own small project, often with a "before" and "after" version so you can see what the new feature actually changes.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;What's inside&lt;/h2&gt;

&lt;/div&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Project&lt;/th&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/georgekobaidze/dotnet11-playground/src/Unions" rel="noopener noreferrer"&gt;Unions&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Union types&lt;/td&gt;
&lt;td&gt;A small console login flow that compares the classic class-hierarchy approach with C# 15 unions and compiler-checked exhaustive matching.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;More examples will be added as new features are explored.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Getting started&lt;/h2&gt;

&lt;/div&gt;
&lt;p&gt;You'll need the &lt;a href="https://dotnet.microsoft.com/download/dotnet/11.0" rel="nofollow noopener noreferrer"&gt;.NET 11 SDK&lt;/a&gt;. Then clone the repo and run any example:&lt;/p&gt;
&lt;div class="highlight highlight-source-shell notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;dotnet run --project src/Unions&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Disclaimer&lt;/h2&gt;

&lt;/div&gt;
&lt;p&gt;.NET 11 and C# 15 features may still change. The code here is for learning and experimentation, not production use.&lt;/p&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;p&gt;Follow me on GitHub for more updates!&lt;/p&gt;




&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The Impossible Branch&lt;/li&gt;
&lt;li&gt;
What Is a Union, Anyway?

&lt;ul&gt;
&lt;li&gt;A Union Is a List of Existing Types&lt;/li&gt;
&lt;li&gt;Case Types Convert to the Union Automatically&lt;/li&gt;
&lt;li&gt;Pattern Matching Looks Inside&lt;/li&gt;
&lt;li&gt;The Current Case Lives in &lt;code&gt;Value&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Cases Don't Have to Be Related, or Even Yours&lt;/li&gt;
&lt;li&gt;The Vocabulary&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;C# Is Late to This Party&lt;/li&gt;
&lt;li&gt;
Before and After: Logging In

&lt;ul&gt;
&lt;li&gt;Before C# 15: The Abstract Base Record&lt;/li&gt;
&lt;li&gt;After C# 15: A Union&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
How C# Unions Actually Work

&lt;ul&gt;
&lt;li&gt;Declaring a Union&lt;/li&gt;
&lt;li&gt;Pattern Matching Unwraps for You&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
Do We Actually Need Them?

&lt;ul&gt;
&lt;li&gt;The Case for Unions&lt;/li&gt;
&lt;li&gt;The Case Against (Or at Least, for Caution)&lt;/li&gt;
&lt;li&gt;My Take&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Wrapping Up&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The Impossible Branch
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;_ =&amp;gt; throw new InvalidOperationException("Unsupported type")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Does this piece of code look familiar to you? I bet it does.&lt;/p&gt;

&lt;p&gt;Sometimes it ends up being a serious problem. Someone adds a new case, the compiler stays quiet, and the "impossible" branch runs.&lt;/p&gt;

&lt;p&gt;For years, we worked around this with abstract base classes, marker interfaces, and libraries like &lt;strong&gt;OneOf&lt;/strong&gt;. They all worked, well... kind of. None of them let the compiler say: &lt;strong&gt;"this value is exactly one of these types, and you forgot one."&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With .NET 11, released on November 10, 2026, that changes. C# 15 ships with a &lt;code&gt;union&lt;/code&gt; keyword, and it's no longer a preview feature. In this article, we'll look at what unions are, where they come from, how they work in C#, and whether you should actually use them.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is a Union, Anyway?
&lt;/h2&gt;

&lt;p&gt;A union is a type whose value is &lt;strong&gt;EXACTLY&lt;/strong&gt; one of a fixed set of options. Not two at once, not something outside the list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fv4tii18tcadruo0m7qz7.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fv4tii18tcadruo0m7qz7.jpeg" alt="Traffic lights" width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Think of a traffic light. It's &lt;strong&gt;red&lt;/strong&gt;, &lt;strong&gt;yellow&lt;/strong&gt;, or &lt;strong&gt;green&lt;/strong&gt;. It's never red and green at the same time, and it's never purple. If you write code that reacts to a traffic light, you want someone to warn you when you forget about yellow.&lt;/p&gt;

&lt;p&gt;A C# 15 union is a type that holds exactly one value from a fixed list of case types.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Union Is a List of Existing Types
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;union&lt;/span&gt; &lt;span class="nc"&gt;Pet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Pet&lt;/code&gt; is the union type. &lt;code&gt;Cat&lt;/code&gt; and &lt;code&gt;Dog&lt;/code&gt; are its case types. They're ordinary records that you can use on their own, and they don't inherit from &lt;code&gt;Pet&lt;/code&gt; or from each other.&lt;/p&gt;

&lt;h3&gt;
  
  
  Case Types Convert to the Union Automatically
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;Pet&lt;/span&gt; &lt;span class="n"&gt;pet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Max"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// implicit conversion from Dog&lt;/span&gt;
&lt;span class="n"&gt;pet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Ruby"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// now it holds a Cat&lt;/span&gt;

&lt;span class="c1"&gt;// Pet other = "Max";       // error: string is not a case type of Pet&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A union holds one case at a time. Assigning a new value replaces the old one, and only the listed case types are accepted.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pattern Matching Looks Inside
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pet&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"Good boy, {d.Name}!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Cat&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"Hello, {c.Name}."&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Dog d&lt;/code&gt; tests the value stored inside the union, so you never unwrap it yourself. And because the list of cases is fixed, the switch is complete without a &lt;code&gt;_&lt;/code&gt; arm.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Current Case Lives in &lt;code&gt;Value&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// Cat { Name = Ruby }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every union has a &lt;code&gt;Value&lt;/code&gt; property of type &lt;code&gt;object?&lt;/code&gt; that holds the current case. You'll rarely touch it directly, because pattern matching reads it for you.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cases Don't Have to Be Related, or Even Yours
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;union&lt;/span&gt; &lt;span class="nc"&gt;IntOrString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;IntOrString&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"ACME-77"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A case type can be almost anything: built-in types, types from other libraries, interfaces, generic type parameters, even other unions. No shared base type is required.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Vocabulary
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Term&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Union type&lt;/td&gt;
&lt;td&gt;The type declared with &lt;code&gt;union&lt;/code&gt;, for example &lt;code&gt;Pet&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Case type&lt;/td&gt;
&lt;td&gt;One of the types in the list, for example &lt;code&gt;Cat&lt;/code&gt; or &lt;code&gt;Dog&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Union conversion&lt;/td&gt;
&lt;td&gt;The implicit conversion from a case type to the union&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Value&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The property that holds the current case, typed &lt;code&gt;object?&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Exhaustive switch&lt;/td&gt;
&lt;td&gt;A &lt;code&gt;switch&lt;/code&gt; that handles every case type, so no &lt;code&gt;_&lt;/code&gt; arm is needed&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In one sentence: a union tells the compiler what a value can be, and the compiler makes sure you handle every option.&lt;/p&gt;




&lt;h2&gt;
  
  
  C# Is Late to This Party
&lt;/h2&gt;

&lt;p&gt;Unions have been on the C# wishlist for a very long time. They went through years of proposals and design meetings, landed in .NET 11 Preview 2 in early 2026, and are now stable.&lt;/p&gt;

&lt;p&gt;Most modern languages already have some form of sum type. Languages borrow good ideas from each other all the time, so it was only a matter of time before C# picked this one up. Because better late than never as they say.&lt;/p&gt;




&lt;h2&gt;
  
  
  Before and After: Logging In
&lt;/h2&gt;

&lt;p&gt;Let's take an example everyone has seen: a login. A login attempt has three possible outcomes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The user is logged in, and we get their user name.&lt;/li&gt;
&lt;li&gt;The user name or password is wrong, and we get the number of attempts left.&lt;/li&gt;
&lt;li&gt;The account is locked, and we get the time it unlocks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Before C# 15: The Abstract Base Record
&lt;/h3&gt;

&lt;p&gt;This is the commonly used pattern:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;LoginResult&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;LoggedIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;UserName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LoginResult&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;InvalidCredentials&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nc"&gt;AttemptsLeft&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LoginResult&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;AccountLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DateTimeOffset&lt;/span&gt; &lt;span class="nc"&gt;Until&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LoginResult&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AuthService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nc"&gt;MaxAttempts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nc"&gt;Dictionary&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_passwords&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"ada"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"IWroteCodeBeforeComputersExisted"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"linus"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"TalkIsCheapShowMeTheCode"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nc"&gt;Dictionary&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_failedAttempts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;LoginResult&lt;/span&gt; &lt;span class="nc"&gt;Login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;userName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;failed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_failedAttempts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetValueOrDefault&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;failed&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nc"&gt;MaxAttempts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AccountLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DateTimeOffset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AddMinutes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;userExists&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_passwords&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TryGetValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;expectedPassword&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;userExists&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;expectedPassword&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;_failedAttempts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;userName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;failed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InvalidCredentials&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MaxAttempts&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;failed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;_failedAttempts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;LoggedIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And a console app that uses it. It keeps asking for credentials until you press &lt;code&gt;Ctrl+C&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;authService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AuthService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"User name: "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;userName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Password: "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userName&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// the input stream was closed (Ctrl+Z on Windows, Ctrl+D on macOS/Linux)&lt;/span&gt;

    &lt;span class="nc"&gt;LoginResult&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;authService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;LoggedIn&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"Welcome back, {l.UserName}!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;InvalidCredentials&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"Wrong user name or password. {c.AttemptsLeft} attempts left."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;AccountLocked&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"Too many attempts. Try again after {a.Until:t}."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;UnreachableException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Unknown error"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works. But look at that last arm. The compiler has no idea that &lt;code&gt;LoginResult&lt;/code&gt; has only three subtypes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fn1zblolex0b30l8ob5cq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fn1zblolex0b30l8ob5cq.png" alt="With Discard Arm" width="800" height="169"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Anyone, in any assembly, can write &lt;code&gt;record LoggedOut : LoginResult&lt;/code&gt;. So the compiler gives us a fair warning to add a discard arm, and the discard arm swallows every case we forget.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff09x6mttk5z3n6onkqvv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff09x6mttk5z3n6onkqvv.png" alt="Without Discard Arm" width="800" height="262"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now imagine that six months later, the security team adds two-factor authentication:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;TwoFactorRequired&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Method&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LoginResult&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// In AuthService:&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nc"&gt;HashSet&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_twoFactorUsers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"linus"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// In Login, right after the password check succeeds:&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_twoFactorUsers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TwoFactorRequired&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Authenticator app"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The project compiles with zero warnings. The first user who turned on two-factor types the correct password, and the whole app crashes with an &lt;code&gt;UnreachableException&lt;/code&gt; instead of asking for a code. It turns out it was reachable.&lt;/p&gt;

&lt;p&gt;There are other small costs too:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every case type must inherit from &lt;code&gt;LoginResult&lt;/code&gt;. You can't use types you don't own, like &lt;code&gt;string&lt;/code&gt; or &lt;code&gt;Exception&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The base class exists only to group things. It has no behavior.&lt;/li&gt;
&lt;li&gt;The same record can't easily belong to two different groups.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  After C# 15: A Union
&lt;/h3&gt;

&lt;p&gt;Here's the same code with a union (spoiler alert: it's much better):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;LoggedIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;UserName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;InvalidCredentials&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nc"&gt;AttemptsLeft&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;AccountLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DateTimeOffset&lt;/span&gt; &lt;span class="nc"&gt;Until&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;union&lt;/span&gt; &lt;span class="nc"&gt;LoginResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LoggedIn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;InvalidCredentials&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;AccountLocked&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// AuthService stays exactly the same, line for line.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;AuthService&lt;/code&gt; doesn't change at all. Each case type converts to &lt;code&gt;LoginResult&lt;/code&gt; implicitly, so &lt;code&gt;return new InvalidCredentials(...)&lt;/code&gt; just works.&lt;/p&gt;

&lt;p&gt;The calling code loses its discard arm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;authService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AuthService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"User name: "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;userName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Password: "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userName&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// the input stream was closed (Ctrl+Z on Windows, Ctrl+D on macOS/Linux)&lt;/span&gt;

    &lt;span class="nc"&gt;LoginResult&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;authService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;LoggedIn&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"Welcome back, {l.UserName}!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;InvalidCredentials&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"Wrong user name or password. {c.AttemptsLeft} attempts left."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;AccountLocked&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"Too many attempts. Try again after {a.Until:t}."&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The compiler knows the list is complete, so the switch is exhaustive without &lt;code&gt;_&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now let's add two-factor authentication:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;TwoFactorRequired&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Method&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;union&lt;/span&gt; &lt;span class="nc"&gt;LoginResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LoggedIn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;InvalidCredentials&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;AccountLocked&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;TwoFactorRequired&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Plus the same _twoFactorUsers field and check in AuthService as before.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time, every switch over &lt;code&gt;LoginResult&lt;/code&gt; that doesn't handle &lt;code&gt;TwoFactorRequired&lt;/code&gt; gets a compiler warning. You see every place you need to update before the code ships, not after the app crashes for the first two-factor user.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fpsx1l45cc2ns5sbxbd83.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fpsx1l45cc2ns5sbxbd83.png" alt="Error" width="800" height="252"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add the missing arm, and Linus gets asked for his code instead of crashing the app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;TwoFactorRequired&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; $&lt;span class="s2"&gt;"Password accepted. Enter the code from your {t.Method}."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdcb20eufgac14m4blybi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdcb20eufgac14m4blybi.png" alt="Error Gone" width="800" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want that warning to fail the build, add this to your project file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;WarningsAsErrors&amp;gt;&lt;/span&gt;CS8509&lt;span class="nt"&gt;&amp;lt;/WarningsAsErrors&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CS8509 is the "switch expression does not handle all possible values" warning. Turning it into an error is a cheap way to make sure nobody ignores it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fd5sb0h0jgwwrymd9xum6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fd5sb0h0jgwwrymd9xum6.png" alt="csproj file" width="742" height="422"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fj1kpv3ztapa5bmehet4d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fj1kpv3ztapa5bmehet4d.png" alt="Error1" width="799" height="323"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  How C# Unions Actually Work
&lt;/h2&gt;

&lt;p&gt;The syntax is small, but there are a few rules worth knowing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Declaring a Union
&lt;/h3&gt;

&lt;p&gt;A union is the &lt;code&gt;union&lt;/code&gt; keyword, a name, and a list of case types in parentheses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;union&lt;/span&gt; &lt;span class="nc"&gt;Pet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;union&lt;/span&gt; &lt;span class="nc"&gt;IntOrString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;union&lt;/span&gt; &lt;span class="nc"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Case types can be almost anything: classes, records, structs, interfaces, generic type parameters, nullable types, and even other unions. They don't need a common base type.&lt;/p&gt;

&lt;p&gt;A union can also have a body with helper members:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Meters&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nc"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Feet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nc"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;union&lt;/span&gt; &lt;span class="nc"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Meters&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Feet&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nc"&gt;TotalMeters&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Meters&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;Feet&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.3048&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InvalidOperationException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"The Length has no value."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The body can't add instance fields or auto-properties. A union holds one value, and that's it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pattern Matching Unwraps for You
&lt;/h3&gt;

&lt;p&gt;When you write &lt;code&gt;pet is Dog d&lt;/code&gt; or a &lt;code&gt;Dog d =&amp;gt;&lt;/code&gt; arm in a switch, the compiler checks the value inside the union, not the union itself. You never have to write &lt;code&gt;pet.Value is Dog d&lt;/code&gt;, although you can.&lt;/p&gt;

&lt;p&gt;Two patterns are the exception: &lt;code&gt;var&lt;/code&gt; and &lt;code&gt;_&lt;/code&gt; match the union itself, without unwrapping it. That matters more than it looks.&lt;/p&gt;




&lt;h2&gt;
  
  
  Do We Actually Need Them?
&lt;/h2&gt;

&lt;p&gt;Fair question. C# has survived 25 years without unions, and we've shipped plenty of working software. So let's be honest about both sides.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Case for Unions
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The compiler checks your work.&lt;/strong&gt; This is the big one. Exhaustive matching turns "I hope I updated every switch" into a list of warnings. On a large codebase with several teams, that's worth a lot.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Signatures tell the truth.&lt;/strong&gt; &lt;code&gt;Task&amp;lt;User&amp;gt;&lt;/code&gt; hides the fact that the user might not exist, or that the caller might not be allowed to see it. &lt;code&gt;Task&amp;lt;GetUserResult&amp;gt;&lt;/code&gt; with &lt;code&gt;union GetUserResult(User, NotFound, Forbidden)&lt;/code&gt; says it out loud.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fewer exceptions for expected outcomes.&lt;/strong&gt; "Wrong password" isn't exceptional. It happens every day. Unions let you return it as a value instead of throwing and catching.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No inheritance tax.&lt;/strong&gt; Case types don't need a shared base class, so you can combine types you don't own, like &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;int&lt;/code&gt; or &lt;code&gt;Exception&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One less dependency.&lt;/strong&gt; Libraries like OneOf did a great job filling the gap. Now the language does it natively.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Case Against (Or at Least, for Caution)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Another way to do the same thing.&lt;/strong&gt; C# now has abstract classes, interfaces, closed hierarchies, and unions for modeling "one of these." Teams will need conventions about which to use where.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Boxing.&lt;/strong&gt; Value-type cases are boxed in the generated struct. That's fine for most code, but it matters in performance-critical paths.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The default hole.&lt;/strong&gt; A union struct can be &lt;code&gt;default&lt;/code&gt;, which means empty. Exhaustive doesn't mean bulletproof.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Overuse.&lt;/strong&gt; Not every method needs a union return type. If a method can only fail because of a bug, an exception is still the right tool.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  My Take
&lt;/h3&gt;

&lt;p&gt;Unions are not a revolution, and they won't replace classes or interfaces. They're a precise tool for one specific job: modeling a value that can be one of a known, fixed set of things.&lt;/p&gt;

&lt;p&gt;That job comes up constantly in real code, in API results, workflow states, messages and parsers. Until now, we did it with workarounds the compiler couldn't verify.&lt;/p&gt;

&lt;p&gt;So yes, I think they're needed. Not because we couldn't write the code before, but because now the compiler can tell us when that code is wrong.&lt;/p&gt;




&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;Unions don't let C# do anything it couldn't do before. What they change is who catches the mistakes. With base classes and discard arms, finding the missing case was your job, or your customer's. With unions, it's the compiler's job.&lt;/p&gt;

&lt;p&gt;In the next article, we'll open up the compiled DLL and see what a union really looks like under the hood. So stay tuned!&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Let's stay connected!&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;💼 &lt;a href="https://www.linkedin.com/in/giorgikobaidze/" rel="noopener noreferrer"&gt;Connect with me on LinkedIn&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💻 &lt;a href="https://github.com/georgekobaidze" rel="noopener noreferrer"&gt;Explore my projects on GitHub&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📱 &lt;a href="https://x.com/georgekobaidze" rel="noopener noreferrer"&gt;Follow me on X&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🎥 &lt;a href="https://www.youtube.com/@Pilotronica" rel="noopener noreferrer"&gt;Watch my videos on YouTube&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;a href="https://discord.com/users/571315867426488330" rel="noopener noreferrer"&gt;Let's Chat on Discord&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>news</category>
      <category>programming</category>
    </item>
    <item>
      <title>.NET 11 Unpacked: Why I'm Recommending STS This Time</title>
      <dc:creator>Giorgi Kobaidze</dc:creator>
      <pubDate>Sun, 15 Nov 2026 16:16:45 +0000</pubDate>
      <link>https://dev.to/georgekobaidze/net-11-unpacked-why-im-recommending-sts-this-time-lnj</link>
      <guid>https://dev.to/georgekobaidze/net-11-unpacked-why-im-recommending-sts-this-time-lnj</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;All the code in this series is available on GitHub. Clone it, run it, and try breaking it yourself.&lt;/strong&gt; &lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://assets.dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/georgekobaidze" rel="noopener noreferrer"&gt;
        georgekobaidze
      &lt;/a&gt; / &lt;a href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;
        dotnet11-playground
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      A lightweight playground repository for experimenting with and testing new features introduced in .NET 11 and C# 15.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;.NET 11 &amp;amp; C# 15 Playground&lt;/h1&gt;

&lt;/div&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer" href="https://github.com/georgekobaidze/dotnet11-playground/docs/cover.jpg"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fgeorgekobaidze%2Fdotnet11-playground%2FHEAD%2Fdocs%2Fcover.jpg" alt=".NET 11 &amp;amp; C# 15 Playground"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;A lightweight playground repository for experimenting with and testing new features introduced in &lt;strong&gt;.NET 11&lt;/strong&gt; and &lt;strong&gt;C# 15&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Use it to explore language changes, try out APIs, and validate ideas in isolation without production constraints. Each feature lives in its own small project, often with a "before" and "after" version so you can see what the new feature actually changes.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;What's inside&lt;/h2&gt;

&lt;/div&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Project&lt;/th&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/georgekobaidze/dotnet11-playground/src/Unions" rel="noopener noreferrer"&gt;Unions&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Union types&lt;/td&gt;
&lt;td&gt;A small console login flow that compares the classic class-hierarchy approach with C# 15 unions and compiler-checked exhaustive matching.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;More examples will be added as new features are explored.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Getting started&lt;/h2&gt;

&lt;/div&gt;
&lt;p&gt;You'll need the &lt;a href="https://dotnet.microsoft.com/download/dotnet/11.0" rel="nofollow noopener noreferrer"&gt;.NET 11 SDK&lt;/a&gt;. Then clone the repo and run any example:&lt;/p&gt;
&lt;div class="highlight highlight-source-shell notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;dotnet run --project src/Unions&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Disclaimer&lt;/h2&gt;

&lt;/div&gt;
&lt;p&gt;.NET 11 and C# 15 features may still change. The code here is for learning and experimentation, not production use.&lt;/p&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/georgekobaidze/dotnet11-playground" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;p&gt;Follow me on GitHub for more updates!&lt;/p&gt;




&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What to Expect From This Article&lt;/li&gt;
&lt;li&gt;A Short History of LTS and STS&lt;/li&gt;
&lt;li&gt;Why There Are Two Tracks at All?&lt;/li&gt;
&lt;li&gt;Why Microsoft Extended STS to 24 Months?&lt;/li&gt;
&lt;li&gt;The Catch: The Same End Date&lt;/li&gt;
&lt;li&gt;Why .NET 11 Might Be the Better Migration Target Right Now&lt;/li&gt;
&lt;li&gt;"But It's Not Battle Tested Yet"&lt;/li&gt;
&lt;li&gt;When .NET 10 Is Still the Right Call&lt;/li&gt;
&lt;li&gt;What I'd Recommend, by Situation&lt;/li&gt;
&lt;li&gt;A Pre-Upgrade Checklist&lt;/li&gt;
&lt;li&gt;Wrapping Up&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What to Expect From This Article
&lt;/h2&gt;

&lt;p&gt;For years, my advice on .NET upgrades was simple: plan around LTS releases. Skip the STS ones unless you have a very good reason.&lt;/p&gt;

&lt;p&gt;This November, that advice is not as strong as before. .NET 10 (LTS) and .NET 11 (STS) both reach end of support on November 14, 2028. Same day. One of them is a year newer.&lt;/p&gt;

&lt;p&gt;In this first article of the .NET 11 Unpacked series, I'll walk through where LTS and STS came from, why the two tracks exist, and why, for a lot of teams right now, .NET 11 may be the better upgrade target. I'll also cover when it isn't.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Short History of LTS and STS
&lt;/h2&gt;

&lt;p&gt;The two-track model is almost as old as .NET Core itself, which has been around since 2016. But the rules have changed several times.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;When&lt;/th&gt;
&lt;th&gt;What changed&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Nov 2026&lt;/td&gt;
&lt;td&gt;.NET 11 ships as STS with 2 years of support, ending on the same day as .NET 10 LTS&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sep 2025&lt;/td&gt;
&lt;td&gt;STS support is extended from 18 to 24 months, starting with .NET 9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2020 to 2022&lt;/td&gt;
&lt;td&gt;.NET 5 drops "Core" from the name, the non-LTS track gets 18 months, and it's renamed from "Current" to "Standard Term Support"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2019&lt;/td&gt;
&lt;td&gt;Microsoft commits to a new major version every November: even numbers are LTS, odd numbers are not&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2016&lt;/td&gt;
&lt;td&gt;.NET Core 1.0 defines two release types: LTS and "Current"&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A few things stand out.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STS never meant "Short."&lt;/strong&gt; The official name is Standard Term Support, and &lt;a href="https://dotnet.microsoft.com/en-us/platform/support/policy/dotnet-core" rel="noopener noreferrer"&gt;Microsoft's policy page&lt;/a&gt; still notes that STS releases were previously known as Current releases.&lt;/p&gt;

&lt;p&gt;Most of us (me included) read the "S" as "Short" for years. That small misreading did a lot of damage to how people see these releases.&lt;/p&gt;

&lt;p&gt;STS support has only ever grown. It went from "a few months after the next release" to 18 months, and now to 24. So I guess "standard" is even more suitable wording here.&lt;/p&gt;

&lt;p&gt;By the way, LTS has stayed at three years the whole time. It hasn't changed.&lt;/p&gt;

&lt;p&gt;The 2025 change is the one that matters here. Under the old rule, an STS release was supported for 6 months after its successor shipped. Under the new rule, it's 12 months. That one change lines up the end date of every STS release with the LTS release before it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why There Are Two Tracks at All?
&lt;/h2&gt;

&lt;p&gt;The two tracks serve two kinds of teams. LTS is for teams that want to upgrade as rarely as possible. STS is for teams that want new features and performance gains every single year.&lt;/p&gt;

&lt;p&gt;What the tracks don't differ in is quality standards. The .NET release policies say &lt;strong&gt;LTS and STS releases differ only by support duration&lt;/strong&gt;, and that the team uses the same engineering and release processes for both, including for security, compatibility and reliability. The support policy page puts it even more plainly: the quality of all releases is the same.&lt;/p&gt;

&lt;p&gt;So an STS release isn't a beta, a preview or a "test" version of the next LTS. STS is a completely different concept. It's a full production release with a shorter warranty.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Microsoft Extended STS to 24 Months?
&lt;/h2&gt;

&lt;p&gt;The 2025 extension wasn't really about STS users. It fixed a problem for LTS users (me included).&lt;/p&gt;

&lt;p&gt;More and more of .NET now ships out of band, outside the yearly release: .NET Aspire, Microsoft.Extensions.AI and the C# Dev Kit, for example. Those components sometimes need package versions from the newest yearly release. So a team that thought it was fully on LTS could pull in a few STS packages without noticing. Under the 18-month rule, those packages lost support six months before the LTS runtime they ran on.&lt;/p&gt;

&lt;p&gt;Moving STS to 24 months closed that gap: the STS packages now stay supported exactly as long as the LTS release before them. The side effect is the one this article is about. Every STS release now retires on the same day as the previous LTS.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Catch: The Same End Date
&lt;/h2&gt;

&lt;p&gt;Under the 24-month rule, the STS release that ships a year after an LTS release retires on the same day as that LTS. This November is the first time it happens with a brand-new release on the table.&lt;/p&gt;

&lt;p&gt;Take a look at the chart:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvbfmc3tt32hwd4dw0c8m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvbfmc3tt32hwd4dw0c8m.png" alt="Chart" width="800" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;.NET 10 shipped a full year before .NET 11, yet both stop getting patches on November 14, 2028. &lt;strong&gt;Picking the LTS this cycle buys you no extra support at all.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The chart also shows the other half of the story. .NET 8 and .NET 9 both reached end of support on November 10, 2026. If you're still on either one, you're no longer getting security fixes.&lt;/p&gt;

&lt;p&gt;Act now! Or whenever you have time for addressing the technical debt.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why .NET 11 Might Be the Better Migration Target Right Now
&lt;/h2&gt;

&lt;p&gt;If you haven't started your .NET 10 migration yet, going straight to .NET 11 gets you everything .NET 10 offers, plus a year of improvements, with no shorter runway.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The same end-of-support date. Both versions are supported until November 14, 2028. Choosing .NET 10 buys you zero extra days.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The same support. Same monthly patches on Patch Tuesday, same security fixes, same engineering process. The only thing LTS adds is time, and this cycle it doesn't even add that.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A newer language. .NET 11 ships with C# 15, which brings union types, closed hierarchies, extension indexers, labeled break and continue, and collection expression arguments. Union types alone have been one of the most requested C# features for years.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Runtime, library and SDK improvements. Every release brings JIT, GC and library work. .NET 11 also cut the SDK install size on Linux and macOS, and adds new APIs across compression, Base64, process handling and SIMD.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Better tooling and security hardening. You get the newest SDK, analyzers and Visual Studio support, and every security fix that went into the platform during the past year.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A shorter jump next time. Both versions retire on the same day, so you'll move off either one by November 2028. From .NET 11, the next LTS (.NET 12) is one version away. From .NET 10, it's two.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  "But It's Not Battle Tested Yet"
&lt;/h2&gt;

&lt;p&gt;That's the most common objection, and it's a fair one. Here's why I think it's smaller than it looks.&lt;/p&gt;

&lt;p&gt;It's actually been public for nine months. .NET 11 Preview 1 shipped in February 2026, followed by monthly previews and three release candidates from September onward. Microsoft's support policy describes release candidates as typically being "go-live" builds, supported in production. So teams could run .NET 11 in production for weeks before GA.&lt;/p&gt;

&lt;p&gt;Though, I don't really recommend using preview versions for your production deployments. It's not the best idea. Always wait for the full releases.&lt;/p&gt;

&lt;p&gt;STS isn't a lower quality bar. Same engineering process, same security and compatibility testing as LTS. The label tells you how long it's supported, not how well it's built.&lt;/p&gt;

&lt;p&gt;Your migration takes time anyway. Most teams I know need weeks or months to upgrade a real system. Patches ship every month, so by the time you're in production, .NET 11 will already have a few servicing releases behind it.&lt;/p&gt;

&lt;p&gt;The real risk is your dependencies, not the runtime. A brand-new release rarely breaks because of the runtime itself. It breaks because a library, a tool or a hosting platform hasn't caught up yet. That's something you can check up front (see the checklist below).&lt;/p&gt;

&lt;p&gt;.NET 10 is also new to you. If you've never run .NET 10 in production, it's untested in your system too. Either way, you're testing a new version against your own code.&lt;/p&gt;

&lt;p&gt;If you want an extra safety margin, there's a simple middle ground: start the migration on .NET 11 now, and go to production after the first one or two patch releases.&lt;/p&gt;




&lt;h2&gt;
  
  
  When .NET 10 Is Still the Right Call
&lt;/h2&gt;

&lt;p&gt;.NET 11 isn't the right answer for everyone. Stick with .NET 10 if any of these apply.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your hosting platform only supports LTS. AWS Lambda, for example, has historically offered managed runtimes only for LTS versions of .NET. On an STS release, you'd need a container image or a custom runtime. Check what your cloud services support before you commit.&lt;/li&gt;
&lt;li&gt;A critical dependency hasn't caught up. A library, an APM agent, an ORM provider or an internal package that doesn't support .NET 11 yet can block you on its own.&lt;/li&gt;
&lt;li&gt;Company policy says LTS. Some organizations, especially regulated ones, only allow LTS versions in production. Arguing with that policy may cost more than the upgrade saves.&lt;/li&gt;
&lt;li&gt;You run on older hardware. .NET 11 updates its minimum hardware requirements on some platforms. Check them if you deploy to older machines.&lt;/li&gt;
&lt;li&gt;You're already well into a .NET 10 migration. Finish it. You'll have the same end date, and moving from 10 to 11 later is a small step if you want it.&lt;/li&gt;
&lt;li&gt;You use .NET MAUI with the old compatibility package. The Xamarin.Forms compatibility package is gone in .NET 11, so that migration comes first.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What I'd Recommend, by Situation
&lt;/h2&gt;

&lt;p&gt;Here's how I'd decide, depending on where your team is today.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Where you are&lt;/th&gt;
&lt;th&gt;My recommendation&lt;/th&gt;
&lt;th&gt;Why&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;On .NET 8 or 9, migration not started&lt;/td&gt;
&lt;td&gt;Go straight to .NET 11&lt;/td&gt;
&lt;td&gt;Same end date as .NET 10, a year newer, and you're already out of support, so start now&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;On .NET 8 or 9, mid-migration to .NET 10&lt;/td&gt;
&lt;td&gt;Finish on .NET 10&lt;/td&gt;
&lt;td&gt;Getting back into support comes first; 10 to 11 is a small step later&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Already on .NET 10&lt;/td&gt;
&lt;td&gt;Stay, and move to 11 only for a feature you need&lt;/td&gt;
&lt;td&gt;No support gain from moving; plan the jump to .NET 12 LTS instead&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Starting a new project&lt;/td&gt;
&lt;td&gt;.NET 11&lt;/td&gt;
&lt;td&gt;Newest language and libraries, same runway&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;LTS-only platform or policy&lt;/td&gt;
&lt;td&gt;.NET 10&lt;/td&gt;
&lt;td&gt;The constraint decides, not the calendar&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Long-lived system you rarely touch&lt;/td&gt;
&lt;td&gt;.NET 10 now, .NET 12 next&lt;/td&gt;
&lt;td&gt;Upgrade only between LTS versions, at your own pace&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;If you're on .NET 8 or 9, the most important point isn't 10 versus 11. Both versions went out of support on November 10, 2026, so you're running without security patches right now. Pick a target and start.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  A Pre-Upgrade Checklist
&lt;/h2&gt;

&lt;p&gt;Before you change &lt;code&gt;TargetFramework&lt;/code&gt; to &lt;code&gt;net11.0&lt;/code&gt;, work through these:&lt;/p&gt;

&lt;p&gt;✅ Hosting: confirm every place you deploy (cloud services, serverless, container base images, CI agents) supports .NET 11.&lt;br&gt;
✅ Dependencies: list your NuGet packages (&lt;code&gt;dotnet list package --outdated&lt;/code&gt; is a good start) and confirm the critical ones support .NET 11.&lt;br&gt;
✅ Tooling: check your IDE, analyzers, test runners, profilers and APM agents.&lt;br&gt;
✅ Hardware: compare your oldest production machines against the .NET 11 minimum requirements.&lt;br&gt;
✅ Breaking changes: read the breaking-change notes for every version you're skipping, not only .NET 11.&lt;br&gt;
✅ Pin the SDK: update &lt;code&gt;global.json&lt;/code&gt; and your CI images so everyone builds with the same SDK.&lt;br&gt;
✅ Test for real: run the full test suite, plus load or benchmark tests on your hot paths.&lt;br&gt;
✅ Roll out gradually: staging first, then a canary or a small slice of traffic.&lt;br&gt;
✅ Plan the exit: put November 2028 in your roadmap now and decide when you'll move to .NET 12.&lt;/p&gt;




&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;"Always pick LTS" is still a good default. But it's a rule of thumb, not a law, and this year the numbers say something different. .NET 10 and .NET 11 retire on the same day, they get the same support, and they're built to the same standard. One of them is simply a year newer.&lt;/p&gt;

&lt;p&gt;I really think this is a positive change by the .NET team.&lt;/p&gt;

&lt;p&gt;So if you're about to start an upgrade, look at .NET 11 seriously. Check your platform and dependencies first. If nothing blocks you, you may find that the STS release is the better deal.&lt;/p&gt;

&lt;p&gt;Next in .NET 11 Unpacked, I'll dig into the features themselves, starting with C# 15. Which .NET version are you targeting right now, and why? Let me know in the comments.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Let's stay connected!&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;💼 &lt;a href="https://www.linkedin.com/in/giorgikobaidze/" rel="noopener noreferrer"&gt;Connect with me on LinkedIn&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💻 &lt;a href="https://github.com/georgekobaidze" rel="noopener noreferrer"&gt;Explore my projects on GitHub&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📱 &lt;a href="https://x.com/georgekobaidze" rel="noopener noreferrer"&gt;Follow me on X&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🎥 &lt;a href="https://www.youtube.com/@Pilotronica" rel="noopener noreferrer"&gt;Watch my videos on YouTube&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;a href="https://discord.com/users/571315867426488330" rel="noopener noreferrer"&gt;Let's Chat on Discord&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>news</category>
      <category>performance</category>
    </item>
    <item>
      <title>Engine Off: My 2026 Hackathon Run Is Over</title>
      <dc:creator>Giorgi Kobaidze</dc:creator>
      <pubDate>Sun, 08 Nov 2026 18:41:00 +0000</pubDate>
      <link>https://dev.to/georgekobaidze/engines-off-my-2026-hackathon-run-is-over-2ho8</link>
      <guid>https://dev.to/georgekobaidze/engines-off-my-2026-hackathon-run-is-over-2ho8</guid>
      <description>&lt;p&gt;There's a moment after a race when you turn the engine off and everything goes quiet. That's where I am with competitions this year.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Races --- 5
Wins  --- 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Two Wins and a Dangerous Habit
&lt;/h2&gt;

&lt;p&gt;I won 2 big dev challenges this year, and it got me so hyped that I couldn't stop entering more and more of them. Winning does that to you. You start refreshing the challenges page like crazy.&lt;/p&gt;

&lt;p&gt;Then I found out I hadn't won my latest challenge. Did that slow me down? Not even a little. It made me want the next one even more.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Idea That Never Showed Up
&lt;/h2&gt;

&lt;p&gt;So I set my sights on the next one: the &lt;a href="https://dev.to/challenges/presage-2026-11-04"&gt;Presage SmartSpectra SDK Challenge&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr3smpx9orb6s1codu74v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr3smpx9orb6s1codu74v.png" alt="Challenge" width="800" height="354"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I was fired up to get started, even though I had no idea what to build.&lt;/p&gt;

&lt;p&gt;That's how it usually works. You never know which challenge is coming next, so you come up with an idea on the run. Metal Birds Watch literally started as a word association: &lt;strong&gt;Copilot, Pilot, Airplanes&lt;/strong&gt;. Give me 2 or 3 days and something always clicks.&lt;/p&gt;

&lt;p&gt;Back in May, during the Gemma 4 challenge, though, I had the opposite problem. I had so many ideas for the challenge that I couldn't pick one, and I ended up building an app for exactly that problem.&lt;/p&gt;




&lt;h2&gt;
  
  
  But This Time It's Different
&lt;/h2&gt;

&lt;p&gt;It's been 4 or 5 days and I still have nothing. I've hit the ceiling.&lt;/p&gt;

&lt;p&gt;I don't want to build something just for the sake of the challenge, because even if I won, it wouldn't feel special when I look back at my GitHub repositories. I want to build something I'm proud of, not something AI suggested. That's not how we roll here. If even I don't care about my creation, how do I expect that someone else will?&lt;/p&gt;

&lt;h2&gt;
  
  
  Effort Doesn't Guarantee a Win
&lt;/h2&gt;

&lt;p&gt;There's another thing. After my recent losses, I realized that no matter how much energy and passion you put into a project, it doesn't guarantee a win.&lt;/p&gt;

&lt;p&gt;For my last entry, I wrote every dataset by hand, and I finished the project with one eye patched up after a small surgery on my eyelid. It didn't matter.&lt;/p&gt;

&lt;p&gt;You can drain yourself to the point where you can't think clearly the next day, and it still won't matter. Someone might come up with a better idea, build it better, or make a better video about it.&lt;/p&gt;

&lt;p&gt;The judges only care about the end result, and rightfully so.&lt;/p&gt;




&lt;h2&gt;
  
  
  All In or Not at All
&lt;/h2&gt;

&lt;p&gt;DEV challenges take a huge chunk of my free time, energy, and mental resources. If I commit to one, I have to go all in. There's no other option.&lt;/p&gt;

&lt;p&gt;That was great in the first half of the year, when I was relatively fresh. Now it's a little different. It might be burnout, which I don't really pay attention to because I'm used to it. But I have so many other things going on right now that it's just harder to make time for challenges. I have a lot of personal projects to maintain and new articles and videos to create. I don't want to take any more time from them.&lt;/p&gt;

&lt;p&gt;I've also noticed that people appreciate the things I create on my own even more, with no challenge attached, and I love that. I have so many cool ideas, and I need to ship them before someone else does.&lt;/p&gt;




&lt;h2&gt;
  
  
  Confessions of a Competition Junkie
&lt;/h2&gt;

&lt;p&gt;I'm a competition junkie, though. I love the anticipation of the results and the huge spike of adrenaline and dopamine when I hear I just won. Oh god, I still remember winning those 2.&lt;/p&gt;

&lt;p&gt;Let's recap the year real quick.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Wins
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Metal Birds Watch
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://dev.to/georgekobaidze/metal-birds-watch-copilot-cli-helped-me-watch-planes-without-looking-up-4ha0"&gt;Metal Birds Watch&lt;/a&gt; (GitHub Copilot CLI Challenge). A real-time aircraft tracker that watches the sky (using a public API) for you and notifies you when a plane flies over. It groups users into grid cells for caching, which was something that I came up with, and I'm still extremely proud of.&lt;/p&gt;

&lt;p&gt;Check out the video:&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/fe2eGNbItUk" width="710" height="399"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;h4&gt;
  
  
  NoteRunway
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://dev.to/georgekobaidze/noterunway-because-your-notion-workspace-deserves-an-elite-crew-53bk"&gt;NoteRunway&lt;/a&gt; (Notion MCP Challenge). A cleanup crew for your Notion workspace. Seven tools hunt down duplicates, dead links, stale pages, and the API keys you swore you'd move later, and nothing destructive runs without your approval.&lt;/p&gt;

&lt;p&gt;Check out the video:&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/qtaKv66-8AY" width="710" height="399"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  The Losses
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Sunday DEV Drive
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://dev.to/georgekobaidze/sunday-dev-drive-a-synthwave-driving-experience-through-your-dev-community-articles-5032"&gt;Sunday DEV Drive&lt;/a&gt; (Weekend Challenge) a synthwave driving game where your DEV articles become neon billboards along an endless road. Enter a username and cruise past your own titles and quotes. I built it in a weekend with Three.js and no build tools, and the car is made from 46 boxes.&lt;/p&gt;

&lt;p&gt;Check out the video:&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/m1vRJxHabJw" width="710" height="399"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;h4&gt;
  
  
  NeuralHats
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://dev.to/georgekobaidze/neuralhats-i-put-edward-de-bonos-six-thinking-hats-on-local-llms-using-gemma-4-54mj"&gt;NeuralHats&lt;/a&gt; (Gemma 4 Challenge). Six AI personas, all running locally on Gemma 4, debate any topic using Edward de Bono's Six Thinking Hats. The Blue Hat decides when the debate is over, and a seventh model writes the final report.&lt;/p&gt;

&lt;p&gt;Check out the video:&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/ZpLpYPuNwr0" width="710" height="399"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;h4&gt;
  
  
  Georgian Language Benchmark
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://dev.to/georgekobaidze/georgian-language-benchmark-two-biggest-ai-labs-clash-in-a-language-that-doesnt-play-by-anyones-5b22"&gt;Georgian Language Benchmark&lt;/a&gt; (Kaggle Benchmarking Challenge). Eight OpenAI and Anthropic models against five Georgian language tasks I wrote by hand. They understand Georgian almost perfectly. Producing it correctly is a whole different story.&lt;/p&gt;

&lt;p&gt;Sorry, I don't have a video for this one (yet).&lt;/p&gt;

&lt;p&gt;So 2 wins out of 5. That isn't too bad. I also entered a couple of writing challenges, but I'm not counting those, because you don't really build something new for those.&lt;/p&gt;

&lt;p&gt;I REALLY wanted to make it 3 wins this year, but as they say, you can't win them all. Sometimes you need to lose to adjust your approach and style.&lt;/p&gt;




&lt;h2&gt;
  
  
  Not Done Yet, Haven't Even Started
&lt;/h2&gt;

&lt;p&gt;Am I done with challenges for good? Oh, no! I'm not going anywhere. I'm just getting started.&lt;/p&gt;

&lt;p&gt;The moment I hear about a hackathon where I can build something really good, I'm in, guaranteed. And I'm going for that win with all I've got, down to the last ounce of energy. There's no other way for me to do things.&lt;/p&gt;

&lt;p&gt;But for 2026, the engines are off, but only for hackathons.&lt;br&gt;
Other than that, I'm going to be even more productive. I still have a few projects and articles to create.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm0zkzcam8hzlpepzzhdj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm0zkzcam8hzlpepzzhdj.png" alt="My Setup" width="800" height="1067"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And for those of you who follow me closely, you're in for a treat. I'm cooking up a few good things. Stay tuned.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Let's stay connected!&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;💼 &lt;a href="https://www.linkedin.com/in/giorgikobaidze/" rel="noopener noreferrer"&gt;Connect with me on LinkedIn&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💻 &lt;a href="https://github.com/georgekobaidze" rel="noopener noreferrer"&gt;Explore my projects on GitHub&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📱 &lt;a href="https://x.com/georgekobaidze" rel="noopener noreferrer"&gt;Follow me on X&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🎥 &lt;a href="https://www.youtube.com/@Pilotronica" rel="noopener noreferrer"&gt;Watch my videos on YouTube&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;a href="https://discord.com/users/571315867426488330" rel="noopener noreferrer"&gt;Let's Chat on Discord&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>devchallenge</category>
      <category>community</category>
    </item>
    <item>
      <title>Scared to Publish Your First Technical Article? Don't Be. I'll Be There to Read It</title>
      <dc:creator>Giorgi Kobaidze</dc:creator>
      <pubDate>Wed, 04 Nov 2026 17:11:22 +0000</pubDate>
      <link>https://dev.to/georgekobaidze/scared-to-publish-your-first-technical-article-dont-be-ill-be-there-to-read-it-1gg2</link>
      <guid>https://dev.to/georgekobaidze/scared-to-publish-your-first-technical-article-dont-be-ill-be-there-to-read-it-1gg2</guid>
      <description>&lt;p&gt;⚡ In a hurry? Skip straight to the rules ⚡&lt;/p&gt;

&lt;h2&gt;
  
  
  Don't Make the Same Mistake I Did
&lt;/h2&gt;

&lt;p&gt;I have plenty of regrets, and one of them is not starting to write my own content years ago. Better late than never, as they say, but still.&lt;/p&gt;

&lt;p&gt;I want to make sure you don't make the same mistake. Every week I see more people joining this amazing community, and it reminds me how excited I was when I first signed up back in 2018. (If you're curious how I ended up here, I wrote about it in &lt;a href="https://dev.to/devengers/doors-i-walked-through-to-reach-dev-5294"&gt;my previous article&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;Back then, I thought writing was only for seasoned veterans. I couldn't have been more wrong. I know plenty of engineers with years of experience who still can't write a single technical article. Not because they have nothing to say, but because they never tried.&lt;/p&gt;




&lt;h2&gt;
  
  
  Time to Take the Leap
&lt;/h2&gt;

&lt;p&gt;Trust me, you don't need decades of experience. You're fine.&lt;/p&gt;

&lt;p&gt;Your first article will probably feel tricky, awkward and hard. Speaking from experience, though, it gets so much easier after that. And your first one will always be special, no matter how good or bad it turns out. You might cringe at it in a year or two, but that's a special kind of cringe. It's proof of how far you've come a long way since then.&lt;/p&gt;




&lt;h2&gt;
  
  
  Not Sure What to Write About?
&lt;/h2&gt;

&lt;p&gt;You don't need a groundbreaking idea. Some of the best first articles are simple:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A bug that took you way too long to fix, and what finally solved it&lt;/li&gt;
&lt;li&gt;Something you learned this week that you wish you'd known earlier&lt;/li&gt;
&lt;li&gt;How you got into programming&lt;/li&gt;
&lt;li&gt;A small project you built, even if nobody uses it&lt;/li&gt;
&lt;li&gt;A concept that finally clicked for you, explained the way you wish someone had explained it to you&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And so many other ideas that you can think of.&lt;/p&gt;

&lt;p&gt;If it was interesting to you, it'll be interesting to someone else.&lt;/p&gt;




&lt;h2&gt;
  
  
  ✍️ The Rules
&lt;/h2&gt;

&lt;p&gt;Alright, I'm going to be your first reader.&lt;/p&gt;

&lt;p&gt;👉 Write your first article on DEV. Any topic, any length. Short is perfectly fine.&lt;br&gt;
👉 Drop the link in the comments.&lt;br&gt;
👉 Optional: tell me what finally made you hit "Publish".&lt;/p&gt;

&lt;p&gt;I'll read &lt;strong&gt;EVERY SINGLE ONE OF THEM&lt;/strong&gt; and give you honest, thorough feedback. I promise! 🤝 I &lt;strong&gt;always&lt;/strong&gt; deliver whatever I promise.&lt;/p&gt;

&lt;p&gt;One small request: &lt;strong&gt;write it yourself&lt;/strong&gt;. Use AI to check your grammar if you like, but the voice and the story should be yours. That's what makes a first article worth reading.&lt;/p&gt;




&lt;h2&gt;
  
  
  Already a Writer? You're Invited Too
&lt;/h2&gt;

&lt;p&gt;If you've been writing for a while, drop a link to your very first article in the comments, or share one piece of advice you wish someone had given you before you published it. Nothing calms first-time nerves like seeing that everyone started somewhere.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Hall of Fame Is Waiting
&lt;/h2&gt;

&lt;p&gt;A few weeks ago, I asked you to share the article you're most proud of, and it turned into &lt;a href="https://dev.to/georgekobaidze/promise-delivered-welcome-to-the-dev-hall-of-fame-65-authors-65-masterpieces-15fa"&gt;the DEV Hall of Fame&lt;/a&gt;: 65 authors, 65 masterpieces. If you start now, your work could earn a spot when I put together the next volume.&lt;/p&gt;

&lt;p&gt;Let's get the journey started! 🚀&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Let's stay connected!&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;💼 &lt;a href="https://www.linkedin.com/in/giorgikobaidze/" rel="noopener noreferrer"&gt;Connect with me on LinkedIn&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💻 &lt;a href="https://github.com/georgekobaidze" rel="noopener noreferrer"&gt;Explore my projects on GitHub&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📱 &lt;a href="https://x.com/georgekobaidze" rel="noopener noreferrer"&gt;Follow me on X&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🎥 &lt;a href="https://www.youtube.com/@Pilotronica" rel="noopener noreferrer"&gt;Watch my videos on YouTube&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;a href="https://discord.com/users/571315867426488330" rel="noopener noreferrer"&gt;Let's Chat on Discord&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>community</category>
      <category>beginners</category>
      <category>writing</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Doors I Walked Through to Reach DEV</title>
      <dc:creator>Giorgi Kobaidze</dc:creator>
      <pubDate>Mon, 26 Oct 2026 16:46:19 +0000</pubDate>
      <link>https://dev.to/devengers/doors-i-walked-through-to-reach-dev-5294</link>
      <guid>https://dev.to/devengers/doors-i-walked-through-to-reach-dev-5294</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Knock, Knock: The First Domino&lt;/li&gt;
&lt;li&gt;The Numbers and a Plot Twist&lt;/li&gt;
&lt;li&gt;How Did I End Up Here?&lt;/li&gt;
&lt;li&gt;Door #1: Let Go for Being Too Good&lt;/li&gt;
&lt;li&gt;Door #2: The Interview I Tried to Write in C++&lt;/li&gt;
&lt;li&gt;Door #3: The Call&lt;/li&gt;
&lt;li&gt;Door #4: The Community I Didn't Know I Needed&lt;/li&gt;
&lt;li&gt;Door #5: The Register Button&lt;/li&gt;
&lt;li&gt;The Door I'm Standing In Now&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Knock, Knock: The First Domino
&lt;/h2&gt;

&lt;p&gt;A few months ago, my DEV buddy &lt;a class="mentioned-user" href="https://dev.to/francistrdev"&gt;@francistrdev&lt;/a&gt; invited me to join DEVengers. Of course, I accepted. It's a great team full of cool folks, they're all stars and it's a real honor to be among them.&lt;/p&gt;

&lt;p&gt;The thing is, a team like this only stays alive if people keep showing up and writing. This is my first post under DEVengers, so I wanted it to be special: easy to read, but with some depth. Consider it the first domino. I'm knocking it over, and I fully expect the rest of the team to follow. (No pressure, folks.)&lt;/p&gt;




&lt;h2&gt;
  
  
  The Numbers and a Plot Twist
&lt;/h2&gt;

&lt;p&gt;So here I am: thousands of followers, almost 50 articles, 2 hackathon wins, 6 Top 7 of the Week features, and that's only scratching the surface of what I've accomplished since I joined this amazing community.&lt;/p&gt;

&lt;p&gt;You might think I've been creating content here for years. Nuh-uh. It's been just a year and a half since I started writing for real.&lt;/p&gt;

&lt;p&gt;Here's the twist: I first heard about DEV back in 2017. Yes, some of you were probably still in high school. That was also around the time I started my professional software engineering career. A year later, in 2018, I registered.&lt;/p&gt;

&lt;p&gt;So basically I bought the ticket in 2018 and boarded the train seven years later.&lt;/p&gt;




&lt;h2&gt;
  
  
  How Did I End Up Here?
&lt;/h2&gt;

&lt;p&gt;To be honest, there's no funny story about how I discovered DEV. No viral post, no friend or colleague dragging me in.&lt;/p&gt;

&lt;p&gt;So instead, I'm going to do what Ted Mosby did in &lt;strong&gt;&lt;em&gt;How I Met Your Mother&lt;/em&gt; TV show.&lt;/strong&gt; He sat his kids down to tell them how he met their mother, and ended up telling them his entire life story first.&lt;/p&gt;

&lt;p&gt;So grab a seat, fellow devs. This is the story of how I met DEV, and all the doors I had to go through before I finally stepped through this one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flc9z1e3pidnmdlweoyjy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flc9z1e3pidnmdlweoyjy.gif" alt="Ted Mosby Professor" width="498" height="278"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Don't worry, though. Unlike Ted, I promise it won't take nine seasons.&lt;/p&gt;




&lt;h2&gt;
  
  
  Door #1: Let Go for Being Too Good
&lt;/h2&gt;

&lt;p&gt;Before I landed my first job as a junior software engineer, I worked in network and IT support while studying at university.&lt;/p&gt;

&lt;p&gt;Yeah, that's the reality in my home country. You can't just focus on your studies and then glide smoothly into a job. Most students are broke as hell, and I definitely was one of them. Even with a job, I barely had enough money for public transport and food.&lt;/p&gt;

&lt;p&gt;At first, the job was fine. It paid (a little), and it left me enough time for my studies. But the longer I stayed, the more I realized how much I hated it. I still performed like crazy, because that's just who I am. I don't know how to do things halfway, even things I can't stand.&lt;/p&gt;

&lt;p&gt;And then I was let go. The reason? I was "overqualified", according to the CEO, or whatever he called himself.&lt;/p&gt;

&lt;p&gt;Back then, I was angry and stressed. In hindsight, maybe he was right. And to be honest, I'm glad he did that, because that's what finally pushed me toward the software engineering job I'd been dreaming about. The last thing I wanted was to get stuck there.&lt;/p&gt;

&lt;p&gt;Turns out, sometimes a door opens because someone kicks you out of another one.&lt;/p&gt;




&lt;h2&gt;
  
  
  Door #2: The Interview I Tried to Write in C++
&lt;/h2&gt;

&lt;p&gt;A cold winter day. I'm sitting in a university building, not much warmer than the street outside, pretending to be a student. Well, technically I was a student, just not at that building of the university.&lt;/p&gt;

&lt;p&gt;I was there because the building was close to the office where I had an interview and a coding assignment. And me being me, I showed up almost an hour early. It was freezing, and I didn't have the money to sit in a McDonald's, so the university building became my shelter. At least my brain wouldn't freeze. I was going to need it.&lt;/p&gt;

&lt;p&gt;I was nervous as hell. Being a junior developer trying to break into the industry is brutal. You know you're not that good, and you're convinced everyone expects you to be great. It's nerve-wracking.&lt;/p&gt;

&lt;p&gt;Then it was time. I walked into the office, and they pointed me to a desk where I'd spend the next hour and a half or so solving five coding problems.&lt;/p&gt;

&lt;p&gt;In C#...&lt;/p&gt;

&lt;p&gt;I didn't even know how to declare a property in C#. Then I saw something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;Total&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I figured it had something to do with getters and setters. I knew getters and setters... but in C++, where you write them out explicitly. What on earth was this?!&lt;/p&gt;

&lt;p&gt;"Alright, Giorgi, that's it. It's over. It's all over, there goes your hopes of becoming a developer" I thought.&lt;/p&gt;

&lt;p&gt;I was close to giving up. Instead, I worked up the courage to interrupt my future co-workers, who were clearly busy with their own work:&lt;/p&gt;

&lt;p&gt;"Umm... excuse me. I know this is C#, and I have some basic knowledge of it, but most of my classes were in C++. Can I possibly write the solutions in C++?"&lt;/p&gt;

&lt;p&gt;You can't imagine my relief when they said the language didn't matter. Oh god.&lt;/p&gt;

&lt;p&gt;I solved four of the five problems, clean. I couldn't get the fifth one running, but I explained my approach and they seemed fine with it.&lt;/p&gt;

&lt;p&gt;I walked out of that office completely drained. I was sure they'd never call me back, and I was already looking at other companies.&lt;/p&gt;




&lt;h2&gt;
  
  
  Door #3: The Call
&lt;/h2&gt;

&lt;p&gt;About a week later, my phone rang.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;-"Hello?"&lt;br&gt;
-"Hello. Is this Giorgi?"&lt;br&gt;
-"Yes?"&lt;br&gt;
-"You completed an assignment at our company. If you're still interested, we'd really like to talk to you. Could you come to our office?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;They wanted to meet me in person. That could only mean one thing: they were interested. If it were a rejection, they'd have just told me over the phone. I was so hyped.&lt;/p&gt;

&lt;p&gt;I went in, we talked everything through, and they offered me a salary that was even lower than what I'd made at my previous job.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;BUT WHO CARED?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I would've taken even less. A whole ocean of the industry was waiting behind that door, with SO MANY OPPORTUNITIES and I'd have walked through it. That's exactly what I needed - the opportunities.&lt;/p&gt;

&lt;p&gt;I left the meeting and immediately called my mom. What I said to her is what I now call the turning point of my life:&lt;/p&gt;

&lt;p&gt;"Hey Mom, your son is officially a software engineer. How cool is that?"&lt;/p&gt;




&lt;h2&gt;
  
  
  Door #4: The Community I Didn't Know I Needed
&lt;/h2&gt;

&lt;p&gt;The first few months were extremely tough. My goal was to make software engineering part of who I was and to be good at everything. That stressed me out. Not the hard work itself, but the constant feeling that I wasn't as good as I wanted to be. I was decent, but I wanted to be much, much better, which is impossible with just a few months of experience. You can only do so much in that time.&lt;/p&gt;

&lt;p&gt;I didn't really have friends to share that stress with, since I was fully dedicated to work (well I don't really have dozens of friends even now, but I don't really care to be honest), my only escape was a few hours of gaming on weekends.&lt;/p&gt;

&lt;p&gt;The only developer community I knew back then was Stack Overflow, which, as you can imagine, is famously the friendliest place on the internet, especially for juniors... &lt;strong&gt;this is sarcasm, by the way.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I was looking for somewhere I could find people with the same struggles, read good technical articles, and feel like I wasn't the only one in this madness.&lt;/p&gt;

&lt;p&gt;I can't recall exactly how I found DEV or which article brought me there, but I remember how fascinated I was. People discussed topics without trying to prove they were better than everyone else. No attitude. Just a welcoming place. I found so many interesting articles and great authors. It was like finding treasure. One Saturday, I spent the entire day at my laptop, reading article after article.&lt;/p&gt;

&lt;p&gt;"That's exactly what I need in my life", I thought.&lt;/p&gt;




&lt;h2&gt;
  
  
  Door #5: The Register Button
&lt;/h2&gt;

&lt;p&gt;I didn't feel worthy of registering, let alone writing a comment, and an article was completely out of the question. I thought that was something only seasoned masters did.&lt;/p&gt;

&lt;p&gt;I was so wrong.&lt;/p&gt;

&lt;p&gt;Finally, sometime in 2018, I registered, thinking I might join a discussion or two. I'd occasionally send articles to my coworkers, and they loved them too. That's when DEV became my go-to place for reading whenever I had some free time.&lt;/p&gt;

&lt;p&gt;Back then, I set myself a long-term goal: to become one of the best writers on this platform. Maybe in ten years or so.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Door I'm Standing In Now
&lt;/h2&gt;

&lt;p&gt;I can't call myself one of the best here. The competition is way too strong. But I think my 21-year-old self would be proud. I finally learned to write about my personal projects, tech analysis, AI, career, everything, and to inspire others to grind more, build more, and show their work to the world.&lt;/p&gt;

&lt;p&gt;Truth be told, I'm doing all this for that young fella who was almost too anxious to ask if he could solve the problems in C++ instead of C#, and too broke to sit in a McDonald's to warm up on a freezing winter day.&lt;/p&gt;

&lt;p&gt;And boy, am I just getting started.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;And that, fellow devs, is how I met the DEV Community.&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Let's stay connected!&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;💼 &lt;a href="https://www.linkedin.com/in/giorgikobaidze/" rel="noopener noreferrer"&gt;Connect with me on LinkedIn&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💻 &lt;a href="https://github.com/georgekobaidze" rel="noopener noreferrer"&gt;Explore my projects on GitHub&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📱 &lt;a href="https://x.com/georgekobaidze" rel="noopener noreferrer"&gt;Follow me on X&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🎥 &lt;a href="https://www.youtube.com/@Pilotronica" rel="noopener noreferrer"&gt;Watch my videos on YouTube&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;a href="https://discord.com/users/571315867426488330" rel="noopener noreferrer"&gt;Let's Chat on Discord&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The DEVengers Have Unfinished Business
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;They were scattered across the world, their names whispered only in legend, until the day the darkness returned and the DEVengers answered.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://the-devengers-tower.github.io/" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkahknj5we2un4ujayiz4.png" alt=" " width="799" height="288"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>career</category>
      <category>community</category>
      <category>writing</category>
      <category>devengers</category>
    </item>
    <item>
      <title>I Earned a Seat at GitHub Universe. It'll Stay Empty, and That's Not Fair</title>
      <dc:creator>Giorgi Kobaidze</dc:creator>
      <pubDate>Sun, 25 Oct 2026 06:44:20 +0000</pubDate>
      <link>https://dev.to/georgekobaidze/i-earned-a-seat-at-github-universe-itll-stay-empty-and-thats-not-fair-3d7k</link>
      <guid>https://dev.to/georgekobaidze/i-earned-a-seat-at-github-universe-itll-stay-empty-and-thats-not-fair-3d7k</guid>
      <description>&lt;h2&gt;
  
  
  Why Am I Writing This?
&lt;/h2&gt;

&lt;p&gt;I'm writing this to show how unfair the world we live in can be, sometimes ridiculously so.&lt;/p&gt;

&lt;p&gt;This isn't the first time I've faced unfairness. It's happened more times than I can count, and by now I'm mostly numb to it. I don't really feel sad or angry anymore. I just keep going.&lt;/p&gt;

&lt;p&gt;So this write-up isn't really for me. It's for people who are just starting out, or who aren't used to doing everything they possibly can, and even more, and still not getting what they deserve. I've seen so many people give up on their careers, their job searches, and their dreams because of moments like this. I was lucky to be resilient and stubborn enough not to let them break me.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Best Prize I'll Never Use
&lt;/h2&gt;

&lt;p&gt;Back in March, I won the biggest competition of my life, a challenge held by GitHub. I built Metal Birds Watch, an app I'm absolutely proud of. It's in maintenance mode right now, but it'll be back soon.&lt;/p&gt;


&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/georgekobaidze/metal-birds-watch-copilot-cli-helped-me-watch-planes-without-looking-up-4ha0" class="crayons-story__hidden-navigation-link"&gt;Metal Birds Watch: Copilot CLI Helped Me Watch Planes Without Looking Up&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
      &lt;a href="https://dev.to/georgekobaidze/metal-birds-watch-copilot-cli-helped-me-watch-planes-without-looking-up-4ha0" class="crayons-article__context-note crayons-article__context-note__feed"&gt;&lt;p&gt;GitHub Copilot CLI Challenge Submission&lt;/p&gt;

&lt;/a&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/georgekobaidze" class="crayons-avatar  crayons-avatar--l  "&gt;
            &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F55651%2F3ad144e9-ca91-4395-b73a-9a0a3d843af9.jpg" alt="georgekobaidze profile" class="crayons-avatar__image"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/georgekobaidze" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Giorgi Kobaidze
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Giorgi Kobaidze
                &lt;a href="/++"&gt;&lt;img alt="Subscriber" class="subscription-icon" src="https://assets.dev.to/assets/subscription-icon-805dfa7ac7dd660f07ed8d654877270825b07a92a03841aa99a1093bd00431b2.png"&gt;&lt;/a&gt;
                
              
              &lt;div id="story-author-preview-content-3242019" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/georgekobaidze" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&gt;
                        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F55651%2F3ad144e9-ca91-4395-b73a-9a0a3d843af9.jpg" class="crayons-avatar__image" alt=""&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Giorgi Kobaidze&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/georgekobaidze/metal-birds-watch-copilot-cli-helped-me-watch-planes-without-looking-up-4ha0" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Feb 13&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/georgekobaidze/metal-birds-watch-copilot-cli-helped-me-watch-planes-without-looking-up-4ha0" id="article-link-3242019"&gt;
          Metal Birds Watch: Copilot CLI Helped Me Watch Planes Without Looking Up
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/devchallenge"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;devchallenge&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/githubchallenge"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;githubchallenge&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/cli"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;cli&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/githubcopilot"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;githubcopilot&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
          &lt;a href="https://dev.to/georgekobaidze/metal-birds-watch-copilot-cli-helped-me-watch-planes-without-looking-up-4ha0" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left"&gt;
            &lt;div class="multiple_reactions_aggregate"&gt;
              &lt;span class="multiple_reactions_icons_container"&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/raised-hands-74b2099fd66a39f2d7eed9305ee0f4553df0eb7b4f11b01b6b1b499973048fe5.svg" width="18" height="18"&gt;
                  &lt;/span&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/fire-f60e7a582391810302117f987b22a8ef04a2fe0df7e3258a5f49332df1cec71e.svg" width="18" height="18"&gt;
                  &lt;/span&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg" width="18" height="18"&gt;
                  &lt;/span&gt;
              &lt;/span&gt;
              &lt;span class="aggregate_reactions_counter"&gt;58&lt;span class="hidden s:inline"&gt;&amp;nbsp;reactions&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/a&gt;
            &lt;a href="https://dev.to/georgekobaidze/metal-birds-watch-copilot-cli-helped-me-watch-planes-without-looking-up-4ha0#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              

              18&lt;span class="hidden s:inline"&gt;&amp;nbsp;comments&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            11 min read
          &lt;/small&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


&lt;p&gt;I still don't know how I finished it. I was in severe pain the entire time. My neck, shoulder, and arm were absolutely killing me, and I could barely sit up straight. It was literal hell. But I pushed through and shipped it anyway.&lt;/p&gt;

&lt;p&gt;So you can imagine how it felt to hear I'd won. And the prizes were amazing.&lt;/p&gt;

&lt;p&gt;One of them was a one-year GitHub Copilot Pro+ subscription. I already had one, so GitHub offered me a very generous voucher for their shop instead. I asked them to donate that money to charity. It's the biggest charitable contribution I'd ever made, and I'm really proud of it.&lt;/p&gt;

&lt;p&gt;The other prize was a ticket to GitHub Universe, the one I was looking forward to most.&lt;/p&gt;

&lt;p&gt;Everything was going great.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Reason I Had Zero Control Over
&lt;/h2&gt;

&lt;p&gt;That lasted until a month or two ago, when I found out I wouldn't be able to attend. The reason is absolutely ridiculous. Sometimes I catch myself laughing hysterically at the sheer level of this utter BS. But the world is a weird place right now. A very, very weird place.&lt;/p&gt;

&lt;p&gt;I won't go into the exact reasons, but remember this: where you are in the world matters, especially at this point in history. It can affect your career, your life, everything. Don't be naive about it. No matter how successful you are, how good you are, or whether you do literally everything right, external factors don't give a damn.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Precious Ticket Gone to Waste
&lt;/h2&gt;

&lt;p&gt;One more thing I'm really annoyed about: the ticket is going to waste, and nobody can use it. I reached out to GitHub twice to ask whether I could transfer it to someone else, but I haven't heard back. I get it, they have a massive conference to run, so they're too busy to respond my emails, but still, I would've loved to give it to someone and make their day. It is what it is. I'm still infinitely grateful to GitHub for what they have done, I love you folks!&lt;/p&gt;

&lt;h2&gt;
  
  
  So What Now?
&lt;/h2&gt;

&lt;p&gt;Nothing, really. Like I said, it's bad, but I've been through much worse, and I can handle this. I have way too much on my plate anyway, so I don't even have time to dwell on it. &lt;/p&gt;

&lt;p&gt;For me, knowing I gave it everything I had, and more, is enough. Regretting that you didn't give it your all would feel far worse than this nonsense.&lt;/p&gt;

&lt;p&gt;This write-up is mainly for people who give up the moment something doesn't go their way. Be stubborn. Focus on what you can control. Play the long game. You might lose a round, but that doesn't mean you've lost the match.&lt;/p&gt;

&lt;p&gt;That's what I've always done, and it played out pretty well for me. So I'm just going to keep doing it. I might get things the hard way, but one way or another, I'll get them. I've lost rounds before. I've never lost the match.&lt;/p&gt;

&lt;p&gt;The seat stays empty this year. It won't stay empty forever. I promise you that.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Let's stay connected!&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;💼 &lt;a href="https://www.linkedin.com/in/giorgikobaidze/" rel="noopener noreferrer"&gt;Connect with me on LinkedIn&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💻 &lt;a href="https://github.com/georgekobaidze" rel="noopener noreferrer"&gt;Explore my projects on GitHub&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📱 &lt;a href="https://x.com/georgekobaidze" rel="noopener noreferrer"&gt;Follow me on X&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🎥 &lt;a href="https://www.youtube.com/@Pilotronica" rel="noopener noreferrer"&gt;Watch my videos on YouTube&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;a href="https://discord.com/users/571315867426488330" rel="noopener noreferrer"&gt;Let's Chat on Discord&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>conference</category>
      <category>github</category>
    </item>
  </channel>
</rss>
