In C#, a string is stored as UTF-16char values. Most characters fit in one char, but many emoji and some rare letters need two (a “surrogate pair”). So "Hi😀".Length is 4, even though a person sees 3 characters.
EnumerateRunes() fixes that for loops. It walks the string one Rune at a time, where a Rune is one whole Unicode code point (a value from U+0000 to U+10FFFF). Use it in a foreach when you need to count, inspect, or filter real characters. A plain GetEnumerator() loop gives char values instead.
This is an instance member of System.String. Call it on a string variable, such as text.EnumerateRunes(...). Calling it on a null variable throws NullReferenceException. It only reads the text. Strings are immutable, so the original string is never changed. It is available in .NET Core 3.0 and later (including .NET 5+). The Rune type lives in System.Text.
Syntax
Use it in a foreach; each item is a System.Text.Rune.
C#
using System.Text;
string text = "Hi\U0001F600"; // "Hi" + a smiling-face emoji
foreach (Rune r in text.EnumerateRunes()) {
Console.WriteLine(r.Value); // 72, 105, 128512
}
Call
What it does
foreach (Rune r in text.EnumerateRunes())
Visit each code point once.
r.Value
The code point as an int (for example 128512 for U+1F600).
r.Utf16SequenceLength
How many chars this rune uses: 1 or 2.
r.ToString()
The rune as a string (one or two chars).
If the string holds a broken surrogate (half of a pair on its own), the enumerator yields Rune.ReplacementChar (U+FFFD) for it instead of throwing. A rune is one code point, which is not always one visible symbol: a letter plus an accent mark, or emoji joined with zero-width joiners, are several runes.
Library declarations
C#
public StringRuneEnumerator EnumerateRunes(); // .NET Core 3.0+
// System.Text
public readonly struct Rune { public int Value { get; } public int Utf16SequenceLength { get; } ... }
Parameters
Parameter
Description
None
EnumerateRunes() takes no parameters.
Return Value
Return Value
Description
StringRuneEnumerator
A lightweight enumerator you use in foreach; each item is a Rune.
C# Examples
Example 1 — Length vs Number of Runes
Compare Length with the number of runes for a string that ends with an emoji.
C#
using System;
using System.Text;
class Program {
static void Main() {
string text = "Hi\U0001F600";
int runes = 0;
foreach (Rune r in text.EnumerateRunes()) {
runes++;
}
Console.WriteLine("Length: " + text.Length);
Console.WriteLine("Runes: " + runes);
}
}
Output
Length: 4
Runes: 3
How It Works
An escaped emoji.\U0001F600 writes the smiling-face emoji by its code point, so the source file stays plain ASCII.
Length counts chars. The emoji is outside the 16-bit range, so it needs two chars. H + i + 2 = 4.
Runes count code points. The loop sees H, i, and the whole emoji: 3 items.
Nothing printed but numbers. The output avoids printing the emoji itself, because not every console font can show it.
Example 2 — Print Each Code Point and Its Size
Show the code point of every rune in hex and how many chars each one uses.
C#
using System;
using System.Text;
class Program {
static void Main() {
string s = "a\u00e9\U0001F600";
foreach (Rune r in s.EnumerateRunes()) {
Console.WriteLine($"U+{r.Value:X4} uses {r.Utf16SequenceLength} char(s)");
}
}
}
Big ones use two. U+1F600 is above U+FFFF, so Utf16SequenceLength is 2.
Example 3 — See the Surrogate Pair That foreach char Shows
Loop over the same string twice: once by char, once by rune, printing the numbers in hex.
C#
using System;
using System.Text;
class Program {
static void Main() {
string s = "A\U0001F600";
foreach (char c in s) {
Console.WriteLine("char: " + ((int)c).ToString("X4"));
}
foreach (Rune r in s.EnumerateRunes()) {
Console.WriteLine("rune: " + r.Value.ToString("X4"));
}
}
}
A char loop splits the emoji. The char loop sees two halves, D83D and DE00. Neither half is a character on its own.
Surrogates have a range. Values from D800 to DFFF are reserved for these halves, which is how .NET knows to join them.
A rune loop joins them.EnumerateRunes() combines the pair into one value, 1F600.
Plain letters agree. For A both loops print 0041; the difference appears only above U+FFFF.
Frequently Asked Questions
It lets you loop over a string one Unicode code point (System.Text.Rune) at a time. Characters stored as surrogate pairs, such as most emoji, come out as one rune instead of two chars.
A System.Text.Rune is a struct that holds one Unicode scalar value, from U+0000 to U+10FFFF (excluding surrogate halves). Its Value property gives the code point as an int.
Strings are UTF-16. Code points above U+FFFF need two char values, called a surrogate pair. Length counts chars, not code points.
.NET Core 3.0 and later, including .NET 5 and newer. It is not available on .NET Framework.
Not always. An accented letter written as two code points, or a family emoji built from several, gives several runes. Use StringInfo for text elements (grapheme clusters).