Is there an easy way to create a multiline string literal in C#?
Here's what I have now:
string query = "SELECT foo, bar" + " FROM table" + " WHERE id = 42";
I know PHP has
<<<BLOCK BLOCK;
Does C# have something similar?
We can use string literal concatenation. Multiple string literals in a row are joined together: char* my_str = "Here is the first line." "Here is the second line."; But wait!
A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string.
In character context, string literals must begin with an ampersand if split in multiple lines.
Raw StringsThey can span multiple lines without concatenation and they don't use escaped sequences. You can use backslashes or double quotes directly.
You can use the @
symbol in front of a string
to form a verbatim string literal:
string query = @"SELECT foo, bar FROM table WHERE id = 42";
You also do not have to escape special characters when you use this method, except for double quotes as shown in Jon Skeet's answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With