NewLines in String Interpolations
SERKUT YILDIRIM
Senior Software Developer @VeriPark | Microsoft MVP | Follow for Daily .NET Dose!
C# supports two types of interpolated strings:
The main difference between these is that a non-verbatim interpolated strings can not contain new lines in its text segments and must instead use escapes (like \r\n). In C# 11, non-verbatim string literals allowed to be in more than one line of code.
The Figure shows us how verbatim and non-verbatim string interpolations are defined. If we use verbatim strings, we have to use the @ mark to write different lines (as in line 5). In C# 10 and previous versions, non-verbatim string literals are defined as on line 9. But in C# 11, non-verbatim string literals are defined as in line 11. We can write on different lines without using escapes (like "\r \n") for non-verbatim strings. This feature makes it easy to perform different operations (Like compare or calculate a thing?etc.) in string expressios (as in line 11).
If we use the new non-verbatim string interpolation definition in C# 10 or previous versions , we get an error like the Figure.
To fix the error, we should open the csproj file of the project and select the language version preview or greater (as in the figure).