NewLines in String Interpolations
String Interpolation Types

NewLines in String Interpolations

C# supports two types of interpolated strings:

  • Verbatim interpolated( $@""): We can create string expressions on more than one line by using the @ mark in verbatim string literals.
  • Non-verbatim interpolated ($""):Using ?escapes (like "\r \n") in non-verbatim string literals we can print the rest of the string expression on a new line.

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.

Bu resim i?in metin sa?lanmad?

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).

Bu resim i?in metin sa?lanmad?

If we use the new non-verbatim string interpolation definition in C# 10 or previous versions , we get an error like the Figure.

Bu resim i?in metin sa?lanmad?

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).

要查看或添加评论,请登录

社区洞察

其他会员也浏览了