Skip to main content

How to Put Quotes Inside a String in C#

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

Inside a C# string, a double quote ends the string, so you can't type one as it is. Put a backslash in front of it, \", and C# reads it as one quote character that belongs to the text.

A string in C# starts and ends with a double quote. The C# programming guide: Quoted string literals start and end with a single double quote character (") on the same line. So a double quote in the middle of the text needs a mark that says "this one is text". That mark is an escape sequence. Microsoft's strings guide: A regular string literal is enclosed in double quotes. Backslash starts an escape sequence, and Common escape sequences include \n (newline), \t (tab), \" (literal quote), \\ (literal backslash), \0 (null char), and Unicode escapes (\uXXXX, \UXXXXXXXX).

Syntax​

Syntax
"<text>\"<quoted text>\"<text>"
$"<text>\"{<expression>}\"<text>"
PartWhat it is
" at the start and at the endStart and end the string. They have no backslash
\"An escape sequence: one double quote character that is part of the text
<text>, <quoted text>The rest of the text
$Makes the string an interpolated string, where {<expression>} puts a value into the text. \" works the same way there
<expression>The value to put between the quotes, such as a variable

The Example​

A console app called QuotesInString prints four sentences with quotes in them: someone saying "hello", a book title in quotes inside an interpolated string, a sentence with an apostrophe, and a short string that is only "Hi" with its two quotes, together with its length.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n QuotesInString
cd QuotesInString
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/QuotesInString/QuotesInString.csproj:
Determining projects to restore...
Restored /Users/you/source/QuotesInString/QuotesInString.csproj (in 45 ms).
Restore succeeded.

dotnet new console -n QuotesInString creates a folder named QuotesInString with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or QuotesInString.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First a quote inside a string:

Program.cs (a quote inside a string)
// \" puts one double quote inside a string.
Console.WriteLine("She said \"hello\" and waved.");

The string starts at the first " and ends at the last one. Each \" between them is one double quote that's part of the text. The C# escape sequence table lists \" as Double quote.

Then an interpolated string:

Program.cs (in an interpolated string)
// It works the same way in an interpolated string.
string title = "The Long Road";
Console.WriteLine($"I'm reading \"{title}\" this week.");

The strings guide: Add a $ prefix to any of the aforementioned literals to get an interpolated string when you need to embed values. \"{title}\" puts a quote, then the value of title, then another quote.

Then a single quote:

Program.cs (a single quote)
// A single quote needs no backslash inside a string.
Console.WriteLine("It's raining.");

A single quote, or apostrophe, doesn't end a string, so It's needs no backslash. The escape sequence table also has \', listed as Single quote, but you need it only for a char, as Common Mistakes shows.

Last, how long a string with quotes is:

Program.cs (how long it is)
// \" is one character: "Hi" with its two quotes is 4 characters long.
string quoted = "\"Hi\"";
Console.WriteLine($"{quoted} is {quoted.Length} characters long.");

"\"Hi\"" is the text "Hi": a double quote, H, i and another double quote. The backslashes only mark the quotes and aren't part of the text, so quoted.Length counts 4 characters.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
She said "hello" and waved.
I'm reading "The Long Road" this week.
It's raining.
"Hi" is 4 characters long.

macOS-style terminal window titled Terminal — QuotesInString showing dotnet run printing She said hello and waved with hello in double quotes, a sentence with the title The Long Road in double quotes, It's raining with its apostrophe, and Hi in double quotes reported as 4 characters long

The output shows the quotes and none of the backslashes: She said "hello" and waved. The interpolated string put the quotes around The Long Road, and It's raining. printed its apostrophe with no backslash in the code. The last line confirms that \" is one character: "Hi" with its two quotes is 4 characters long.

The Complete File​

macOS-style code window titled Program.cs — QuotesInString showing backslash-quote escape sequences in a regular string and in an interpolated string, an apostrophe with no backslash, and a string of a quoted Hi whose Length is printed

Program.cs
// \" puts one double quote inside a string.
Console.WriteLine("She said \"hello\" and waved.");

// It works the same way in an interpolated string.
string title = "The Long Road";
Console.WriteLine($"I'm reading \"{title}\" this week.");

// A single quote needs no backslash inside a string.
Console.WriteLine("It's raining.");

// \" is one character: "Hi" with its two quotes is 4 characters long.
string quoted = "\"Hi\"";
Console.WriteLine($"{quoted} is {quoted.Length} characters long.");

Common Mistakes​

MistakeWhat happensDo this instead
Typing the quotes without a backslash"She said "hello" and waved." stopped the build with error CS1003: Syntax error, ',' expected, twice, because the string ended at the quote before helloWrite \" for each quote inside the string
Doubling the quotes"She said ""hello"" and waved." stopped the build with the same error CS1003: Syntax error, ',' expected, twiceIn a regular string, write \". A doubled "" is a quote only in a verbatim string, which starts with @: @"She said ""hello"" and waved." printed She said "hello" and waved.
Putting a single quote in a char without a backslashchar apostrophe = '''; stopped the build with error CS1011: Empty character literal and error CS1010: Newline in constantWrite '\''. A double quote in a char needs no backslash: '"' printed "

FAQ​

Do I need \' for an apostrophe in a string?​

No. "It\'s raining." == "It's raining." printed True, so both are the same text. A single quote needs a backslash only in a char, which is written between single quotes.

Can I write quotes without any backslashes?​

Yes, with a raw string literal, which starts and ends with three double quotes. The raw string guide: A raw string literal is delimited by three or more double quotes. Inside the delimiters, every character is taken literally. """She said "hello" and waved.""" printed She said "hello" and waved. Because every character counts as text there, a backslash is printed too: """She said \"hello\" and waved.""" printed She said \"hello\" and waved.

Why does a verbatim string use "" and not \"?​

Because a verbatim string, which starts with @, reads a backslash as a plain character. The verbatim string reference: Only a quote escape sequence ("") isn't interpreted literally; it produces one double quotation mark. The raw string guide sums up all three kinds: A regular literal needs escapes for " and \. A verbatim literal still needs "" to embed a quote. A raw literal needs neither.

Conclusion​

To put a double quote inside a regular or interpolated C# string, write \": the backslash marks the quote as text, and the quote counts as one character. An apostrophe needs no backslash in a string, only in a char. When a string has many quotes, a raw string literal lets you type them as they are.

Additional Resources​