Putting \" in verbatim string with C#

prosseek picture prosseek · May 13, 2011 · Viewed 8.3k times · Source

I need to print

a
"b"
c

with the vebatim string, I posed another question about multiple line code template here.

I tried with verbatim string as follows :

using System;

class DoFile {

    static void Main(string[] args) {
        string templateString = @"
        {0}
        \\"{1}\\"
        {2}
        ";
        Console.WriteLine(templateString, "a", "b", "c");
    }
}

But, I got this error.

t.cs(8,11): error CS1525: Unexpected symbol `{'
t.cs(9,0): error CS1010: Newline in constant
t.cs(10,0): error CS1010: Newline in constant

\"{1}\" doesn't work neither.

What's wrong?

Answer

manojlds picture manojlds · May 13, 2011

Try this ( "" instead of " to escape )

string templateString = @"
        {0}
        ""{1}""
        {2}
        ";

From C# specification: http://msdn.microsoft.com/en-us/library/Aa691090

quote-escape-sequence: ""