Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Declare a string that spans on multiple lines

I'm trying to create a string that is something like this

string myStr = "CREATE TABLE myTable
(
id text,
name text
)";

But I get an error: http://i.stack.imgur.com/o6MJK.png

What is going on here?

like image 390
Ben Avatar asked Sep 22 '12 23:09

Ben


2 Answers

Make a verbatim string by prepending an at sign (@). Normal string literals can't span multiple lines.

string myStr = @"CREATE TABLE myTable
(
    id text,
    name text
)";

Note that within a verbatim string (introduced with a @) the backslash (\) is no more interpreted as an escape character. This is practical for Regular expressions and file paths

string verbatimString = @"C:\Data\MyFile.txt";
string standardString = "C:\\Data\\MyFile.txt";

The double quote must be doubled to be escaped now

string verbatimString  = @"This is a double quote ("")";
string standardString  = "This is a double quote (\")";
like image 159
Olivier Jacot-Descombes Avatar answered Sep 20 '22 08:09

Olivier Jacot-Descombes


string myStr = @"CREATE TABLE myTable
(
id text,
name text
)";
like image 42
Ozerich Avatar answered Sep 20 '22 08:09

Ozerich