Unicode and text comparison
C# strings use UTF-16 code units. A char represents one unit, but some Unicode characters use two units, and a visible text element may contain several Unicode scalar values. Choose the measurement that matches your task.
using System;using System.Globalization;using System.Text;string text = "\U0001F600";int scalars = 0;foreach (Rune rune in text.EnumerateRunes()) scalars++;Console.WriteLine(text.Length);Console.WriteLine(scalars);Console.WriteLine(new StringInfo(text).LengthInTextElements);Console.WriteLine(Encoding.UTF8.GetByteCount(text));The four measurements describe different representations of the same text.
Expected output (reasoned, not executed):
2114Three ways to count text
Length counts UTF-16 units. EnumerateRunes() iterates scalar values (using replacement values for malformed UTF-16). StringInfo counts text elements, which approximate user-perceived characters under Unicode segmentation rules. For valid text, the emoji U+1F600 uses two UTF-16 units and one scalar; a base letter plus a combining accent uses two scalars but can form one text element.
Compare text for its purpose
Use ordinal comparisons for protocol tokens and identifiers when the protocol specifies exact matching. OrdinalIgnoreCase is useful when that contract ignores case; it is not a linguistic sort. For human-language display or sorting, choose an appropriate culture. Normalization can make canonically equivalent spellings match, but only adopt it when the application contract permits it.
Encode text as bytes
UTF-8 encodes Unicode into bytes for files and network protocols. ASCII characters use one byte; U+1F600 uses four. Encoding.UTF8.GetByteCount measures bytes, not screen width. Truncating by char index can split a surrogate pair; byte truncation can split an encoded sequence. Neither should be used blindly to enforce a user-visible text limit.
using System;using System.Globalization;string composed = "\u00E9";string decomposed = "e\u0301";Console.WriteLine(decomposed.Length);Console.WriteLine(new StringInfo(decomposed).LengthInTextElements);Console.WriteLine(string.Equals(composed, decomposed, StringComparison.Ordinal));Console.WriteLine(string.Equals(composed.Normalize(), decomposed.Normalize(), StringComparison.Ordinal));The sequences differ before canonical normalization. Default Normalize uses form C.
Expected output (reasoned, not executed):
21FalseTrueusing System;string status = "ready";Console.WriteLine(string.Equals(status, "READY", StringComparison.Ordinal));Console.WriteLine(string.Equals(status, "READY", StringComparison.OrdinalIgnoreCase));The second comparison follows an explicit case-insensitive identifier contract.
Expected output (reasoned, not executed):
FalseTrueCommon mistake and correction
text.Length == 1 does not mean text contains exactly one visible character. text[0] might be only a high surrogate. For user-facing counts use text elements; for storage quotas measure the encoding. Do not normalize passwords or opaque identifiers without a specified policy.
Try it yourself
Predict the UTF-16 length, scalar count, text-element count and UTF-8 byte count of "A\U0001F600", then write a program to report them.
Next: StringBuilder and literal syntax. Examples target C# 14 / net10.0. They are static study material; compiler and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Unicode and text comparison — knowledge check
Apply the lesson concepts to the following scenarios.
How many UTF-16 units represent U+1F600?
Which API counts text elements?
What does ordinal case-sensitive comparison of ready and READY return?
What can canonical normalization do for e plus a combining acute accent?
How many UTF-8 bytes are needed for A followed by U+1F600?