Skip to content

C# Feature - UTF-8 string literals

UTF-8 string literals turn a string literal into UTF-8 bytes directly, which is useful for low-level protocols and performance-sensitive text handling.

C# 11.0 (2022)

byte[] payload = Encoding.UTF8.GetBytes("PING");
ReadOnlySpan<byte> payload = "PING"u8;
  • The result is UTF-8 bytes, not a string.
  • Use this when APIs already consume bytes or spans.