Codectionary / Developer documentation / C#

Understand UTF-16, Rune and Text Elements

Distinguish string code units, Unicode scalar values and user-perceived text elements.

Read the syntax

Distinguish string code units, Unicode scalar values and user-perceived text elements.

EnumerateRunes treats the pair as a single Unicode scalar value.

What it is and why to use it

A .NET string stores UTF-16 code units, and char represents one such unit. A visible character can require two code units or several Unicode scalar values. Rune represents a scalar value, while StringInfo can enumerate text elements. Choosing the right unit matters for counting, truncating and processing international text.

How it works

string.Length counts UTF-16 code units. EnumerateRunes visits scalar values and handles surrogate pairs as one scalar. StringInfo.GetTextElementEnumerator groups text elements according to the runtime’s Unicode handling. A scalar is still not necessarily one displayed glyph: a letter plus a combining mark can form one text element.

Common mistakes and limitations

Do not use char slicing as a general way to truncate visible characters; it can split a surrogate pair. Do not assume Rune count equals screen width or grapheme count. Text rendering and Unicode versions introduce further complexity, so state which unit a limit measures.

Run the examples locally

These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.

Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.

C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.

Create and enter a project

dotnet new console -n CSharpExample --framework net10.0
cd CSharpExample

CSharpExample.csproj settings used for these examples

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <LangVersion>12.0</LangVersion>
    <Nullable>enable</Nullable>
    <ImplicitUsings>disable</ImplicitUsings>
  </PropertyGroup>
</Project>

Build and run after saving one example in Program.cs

dotnet build
dotnet run --no-build

Continue learning

Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.

Syntax

foreach (Rune rune in text.EnumerateRunes()) { /* scalar values */ }

Examples

Count code units and scalar values

Use an ASCII letter followed by a supplementary-plane emoji.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Text;

string text = "A\U0001F600";
int scalars = 0;
foreach (Rune rune in text.EnumerateRunes()) scalars++;
Console.WriteLine(text.Length);
Console.WriteLine(scalars);
  1. A uses one UTF-16 unit and the emoji uses a surrogate pair, so Length is three.
  2. EnumerateRunes treats the pair as a single Unicode scalar value.
  3. The second count is two. This avoids confusing storage units with Unicode scalar values.
  4. Expected output: 3 2
  5. Name the unit used by a length limit.

Recognise a combining sequence

Compare two scalar values with one text element.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Globalization;
using System.Text;

string text = "e\u0301";
int scalars = 0;
foreach (var rune in text.EnumerateRunes()) scalars++;
int elements = 0;
var iterator = StringInfo.GetTextElementEnumerator(text);
while (iterator.MoveNext()) elements++;
Console.WriteLine(scalars);
Console.WriteLine(elements);
  1. The string contains a letter e and a combining acute accent.
  2. Rune enumeration counts the two scalar values separately.
  3. StringInfo recognises their combined text element as one. Neither count is a promise about the number of pixels or terminal columns used.
  4. Expected output: 2 1
  5. Name the unit used by a length limit.

Best practices

  • Name the unit used by a length limit.
  • Use appropriate Unicode APIs rather than assuming one char is one visible character.

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading