Saturday, 17 November 2018

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;


Does C# have something similar?


Answer




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.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...