SHARE:

Introducing C#11: Newlines in string interpolation expressions

Introduction

C# 11 brings an improvement on string interpolation. Previously it was impossible, apart from the use of verbatim syntax with @, to write variable text on several lines (only between curly brackets). In this article I will show you that this is possible by example.

String interpolation in action with C# 11

The following code shows the difference between C# 11 and prior versions of C#:

As you can see prior version of C# would have raised the following compilation error:

Error CS8967 Newlines inside a non-verbatim interpolated string are not supported in C# 10.0. Please use language version preview or greater.

Remember! This is only valid for Newlines between curly braces { newslines here only }, the following won’t work:

Helpful right ? 🙂

Written by

anthonygiretti

Anthony is a specialist in Web technologies (14 years of experience), in particular Microsoft .NET and learns the Cloud Azure platform. He has received twice the Microsoft MVP award and he is also certified Microsoft MCSD and Azure Fundamentals.
%d bloggers like this: