UTF-8 string literals in C# 11

UTF-8 string literals in C# 11 give you a direct way to write UTF-8 data in source code without first creating a normal string and converting it later. If you have searched for u8 literals, UTF-8 string literals, or how they work in modern .NET, the important detail is that they produce ReadOnlySpan<byte> rather than string.

That matters because many high-performance .NET APIs already work with UTF-8 bytes. Using UTF-8 string literals can remove extra encoding steps, reduce allocations, and make low-level networking, serialization, and protocol code cleaner. Here is the basic syntax in C# 11:

// Old way
{
    ReadOnlySpan<byte> utf8String = new byte[] { 72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33 };
    string decodedString = System.Text.Encoding.UTF8.GetString(utf8String);
    Console.WriteLine(decodedString);
}
// Fancy new way
{
    ReadOnlySpan<byte> utf8StringLiteral = "Hello, World!"u8;
    string decodedString = System.Text.Encoding.UTF8.GetString(utf8StringLiteral);
    Console.WriteLine(decodedString);
}

Just look at that syntactic sugar! One important thing to note about UTF-8 string literals is that they are runtime constants, not compile-time constants. This means that their value is determined at runtime and not at compile time. This also means that UTF-8 string literals can’t be combined with string interpolation. You can’t use the $ token and the u8 suffix on the same string expression.

Conclusion

In conclusion, UTF-8 string literals in C# 11 and .NET Core 7 are a great addition to the language and framework. They allow us to handle UTF-8 strings more efficiently and can lead to better performance in our applications. So, next time you’re working on a project, consider using UTF-8 string literals instead of traditional string literals. Your future self will thank you.

If you are exploring other useful C# features in the same generation of the language, I would also look at Custom String Interpolation in C#: InterpolatedStringHandler Explained and Say Hello to the Power of Generic Attributes in C# 11.