Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
C#

C#: Fix a UTF-8 String Literal Behaving Differently in a Switch

- 21.11.26 - ErcanOPAK

🔧 C#: Fix a UTF-8 String Literal Behaving Differently in a Switch

C# 11’s u8 suffix produces a ReadOnlySpan<byte> UTF-8 literal, and pattern matching on spans in a switch expression compares byte sequences directly. If one case literal was typed with a visually-identical but differently-encoded Unicode sequence — a lookalike character, or a precomposed accented letter versus a combining-character sequence that renders the same — the UTF-8 bytes differ even though the literals look identical, so the switch silently falls through to a default case instead of the branch that visually appears to match.

🔎 The Problem

ReadOnlySpan<byte> input = GetUserTag(); // pasted from an external source

var result = input switch
{
    "caf\u00e9"u8 => "precomposed e-acute",      // U+00E9, single code point
    _ => "no match"                              // actually hits this branch
};
// GetUserTag() returned "cafe\u0301"u8 - 'e' + U+0301 combining acute accent.
// Renders identically, encodes to different UTF-8 bytes entirely.

✅ Fix: Compare Bytes Explicitly When a ‘Identical-Looking’ Match Fails

  • When a u8 literal switch isn’t matching a case you can visually confirm is “the same text,” dump both sides as byte arrays and compare them directly, since the bug is specifically about a byte-level difference that text rendering hides.
  • Normalize any string that might contain user-entered or pasted Unicode (via string.Normalize()) before converting it to UTF-8 bytes for comparison, rather than assuming visually identical source-code literals are guaranteed to be byte-identical.
  • Prefer ASCII-only u8 literal cases where correctness matters, and reserve non-ASCII u8 comparisons for contexts where you control both sides end to end and can verify the exact byte sequence once.

🚨 Why This Is Dangerous

  • A switch that silently falls through to the wrong branch because of an invisible byte-level mismatch can route data down an unintended code path with no exception or warning, and the bug can survive code review indefinitely because every literal involved looks correct on screen.

A u8 literal matches by the bytes it actually contains, not by how the character renders in your editor — and two things that look the same on screen aren’t guaranteed to be the same sequence of bytes underneath.

— C# Best Practice

Related posts:

Important Tips To Write Clean Code In C# from Debendra Dash

Important Tips To Write Clean Code In C# from Debendra Dash

Step-by-Step: Building a Modern Console App with C# 13

Step-by-Step: Building a Modern Console App with C# 13

Creating Multiline textbox using Html.Helper function in Asp.Net MVC

Creating Multiline textbox using Html.Helper function in Asp.Net MVC

Views: 7

Post navigation

Photoshop: Fix a Liquify Mesh Snapping Back on a Smart Object
Visual Studio: Fix CodeLens Reference Counts Stuck at Stale Numbers

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,068)
  • Get the User Name and Domain Name from an Email Address in SQL (990)
  • How to add default value for Entity Framework migrations for DateTime and Bool (986)
  • Get the First and Last Word from a String or Sentence in SQL (865)
  • How to select distinct rows in a datatable in C# (859)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (661)
  • Add Constraint to SQL Table to ensure email contains @ (619)
  • Average of all values in a column that are not zero in SQL (576)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (553)
  • Find numbers with more than two decimal places in SQL (481)

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • AÄŸ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store
  • WordPress: Fix a Navigation Block Losing Submenu Items After Sync
  • Visual Studio: Fix CodeLens Reference Counts Stuck at Stale Numbers
  • C#: Fix a UTF-8 String Literal Behaving Differently in a Switch
  • Photoshop: Fix a Liquify Mesh Snapping Back on a Smart Object
  • The AI Prompt That Turns a Crash Dump Into a Short Repro Script

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,068)
  • Get the User Name and Domain Name from an Email Address in SQL (990)
  • How to add default value for Entity Framework migrations for DateTime and Bool (986)
  • Get the First and Last Word from a String or Sentence in SQL (865)
  • How to select distinct rows in a datatable in C# (859)

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • AÄŸ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com