16 November

C# 11 – UTF-8 string literals

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.


Author

Tomasz Juszczak

CTO /

Technical Lead

Tomasz Juszczak

About prog

Founded in 2016 in Warsaw, Poland. Prographers mission is to help the world put the sofware to work in new ways, through the delivery of custom tailored 3D and web applications to match the needs of the customers.


Let's talk

SEND THE EMAIL