Can I escape a double quote in a verbatim string literal?

kdt picture kdt · Dec 18, 2009 · Viewed 405.9k times · Source

In a verbatim string literal (@"foo") in C#, backslashes aren't treated as escapes, so doing \" to get a double quote doesn't work. Is there any way to get a double quote in a verbatim string literal?

This understandably doesn't work:

string foo = @"this \"word\" is escaped";

Answer

Myles picture Myles · Dec 18, 2009

Use a duplicated double quote.

@"this ""word"" is escaped";

outputs:

this "word" is escaped