Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting \" in verbatim string with C# [duplicate]

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?

like image 665
prosseek Avatar asked May 13 '11 18:05

prosseek


People also ask

What is a verbatim string C#?

In C#, a verbatim string is created using a special symbol @. @ is known as a verbatim identifier. If a string contains @ as a prefix followed by double quotes, then compiler identifies that string as a verbatim string and compile that string.

How do you define a string literal as a verbatim string instead of a quoted string?

In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals.

How do you initiate a string without escaping each backslash?

You can use " \\ " instead of " \ " as well as '@' sign in the beginning.

What does the at symbol do in C#?

From the C# documentation it states that the @ symbol is an Identifier. The prefix “@” enables the use of keywords as identifiers, which is useful when interfacing with other programming languages.


1 Answers

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: ""

like image 168
manojlds Avatar answered Oct 05 '22 15:10

manojlds