This article illustrates the different techniques to create a multiline string literal in C#.

1. Using Verbatim String Literal

In C#, the @ symbol serves as a verbatim identifier. It can be used to indicate that a string literal is to be interpreted verbatim. The following example illustrates the effect of defining a verbatim string literal containing line breaks.

Download  Run Code

Output:

<?xml?>
<book>
    <name>Harry Potter</name>
    <author>J. K. Rowling</author>
    <language>English</language>
    <genre>Fantasy</genre>
</book>

 
Starting with C# 6.0, you can use interpolated strings ($) with the verbatim string literal (@):

Download  Run Code

Output:

<?xml?>
<book>
    <name>Harry Potter</name>
    <author>J. K. Rowling</author>
    <language>English</language>
    <genre>Fantasy</genre>
</book>

2. Using String.Join() method

Another alternative is to use the String.Join() method, which concatenates elements of a collection with the specified separator between each element. The following code example demonstrates how to create a multiline string literal with the String.Join() method using Environment.NewLine as a delimiter.

Download  Run Code

Output:

<?xml?>
<book>
    <name>Harry Potter</name>
    <author>J. K. Rowling</author>
    <language>English</language>
    <genre>Fantasy</genre>
</book>

That’s all about creating a multiline string literal in C#.