November 2, 2016

C# string magic

In C#, you need strings often. Less often, you need a long string, but it still happens. So here’s a nice thing to know. You can split them over multiple lines without need the + operator and an Environment.NewLine call.

What you need is a verbatim string literal. Those start with an @:

But the extra nice thing is they can span multiple lines, making them easier to read:

But what if you need to add some variable text to this string? Since C# 6 we can use string interpolation, combined with our verbatim string literal:

This works perfectly and gives us very readable code.

Leave a Reply

Your email address will not be published. Required fields are marked *