Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline string literal in C#

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?

like image 858
Chet Avatar asked Jul 08 '09 20:07

Chet


People also ask

How do I write a multi line string literal in C?

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!

What are literal strings in C?

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.

What operator is used to define a multiline string literal?

In character context, string literals must begin with an ampersand if split in multiple lines.

Can a string have 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.


1 Answers

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.

like image 55
John Rasch Avatar answered Nov 04 '22 22:11

John Rasch