16 November

C# 11 – UTF-8 string literals

Programming

min. read

Reading Time: < 1 minute
C# 11
C# 11

Let’s list all the features that C# 11 brings to the table. Let’s discuss them all one by one, with their use cases, and see how they can come in handy. At the bottom of each article, you can find a link to all 14 new C# features!

UTF-8 string literals

UTF-8 is the encoding that runs the internet. If you have a string coming into your APS.NET application it’s always in UTF-8, whereas by default, .NET and C# convert to UTF-16 using Encoding.Unicode.GetBytes("X");.

To improve upon the conversion and reusability C# team introduced UTF-8 string literals.

ReadOnlySpan<byte> utf8 = "My internet string"u8;

Comparison

Let’s compare how this looks in byte notation upon which we convert it.

ReadOnlySpan<byte> u16 = Encoding.Unicode.GetBytes("X"); // 58 00
ReadOnlySpan<byte> u8 = "X"u8; // 58

As you can see, this saves us a bit of coding, and the result is already efficiently written into proper code.

This is definitely a niche feature, but might be helpful for some folks.

Further reading at dotnet GitHub page.


Let's talk

SEND THE EMAIL

I agree that my data in this form will be sent to [email protected] and will be read by human beings. We will answer you as soon as possible. If you sent this form by mistake or want to remove your data, you can let us know by sending an email to [email protected]. We will never send you any spam or share your data with third parties.

I agree that my data in this form will be sent to [email protected] and will be read by human beings. We will answer you as soon as possible. If you sent this form by mistake or want to remove your data, you can let us know by sending an email to [email protected]. We will never send you any spam or share your data with third parties.