16 November
C# 11 – String interpolated new line
Programming
min. read
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!
String interpolated new line
In the previous releases of C#, we had problems when we tried to create a line break in the middle of a verbatim block.
var variable = "World";
var verbatim = $"Hello {variable.ToLower()}"; // Ok
var error = $"Hello {variable. // <- error before C# 11
ToLower()}";
In new C# 11, you can now make line breaks in the middle of the variables. This will be useful for use cases when the line is too long and you are looking at the place to break it!
This is still one line when you print it!
Still in C# 10, how to do it then?
Before you had only the following options:
var break1 = "Hello " +
$"{variable.ToLower()}";
// This one might be inconvinient as it might print in 2 separate lines.
var break2 = $@"Hello
{variable.ToLower()}";
I like this change, it’s convenient!
Further reading at dotnet GitHub page.
Let's talk
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.