How to include an apostrophe in a C# string

Add an apostrophe to a C# string correctly: when escaping is needed, char literals, verbatim and raw strings, curly apostrophes, and how to check the result.

Short answer: inside an ordinary double-quoted C# string, an apostrophe needs no special treatment. You type it as is. Escaping only matters when the apostrophe is a single char value, when you want a typographic (curly) apostrophe, or when the string is passed to another system, such as a database, that has its own quoting rules.

What the apostrophe means to the C# compiler

C# uses double quotes to delimit string literals and single quotes to delimit char literals. A char literal is a value that holds exactly one character. The straight apostrophe on a standard keyboard is the Unicode character U+0027. It only has a special meaning inside a char literal. Inside a string, it is ordinary text.

Step 1: Type the apostrophe directly in a regular string

string message = "It's ready.";
Console.WriteLine(message);

Expected output: It's ready.
The escape sequence ' is also valid in a regular string literal and produces the same character. That means "It's ready." is equivalent to the example above. The backslash is unnecessary there, so most code leaves it out.

Step 2: Escape it when you need a single char

A char literal is wrapped in single quotes, so an apostrophe inside one must be escaped with a backslash. You can also write it as the Unicode escape \u0027. Writing three apostrophes in a row (''') is rejected by the compiler.

char apostrophe = '\'';
char sameThing = '\u0027';
string word = "don" + apostrophe + "t";   // don't

Apostrophes in verbatim, interpolated and raw strings

  • Verbatim strings (@"..."): type the apostrophe as is. Backslashes are taken literally, so @"It's" keeps the backslash and produces It's. Inside a verbatim string, only a double quote needs special handling, and you write it as two double quotes.
  • Interpolated strings ($"..."): type the apostrophe as is, including right after a placeholder.
  • Raw string literals ("""...""", available from C# 11): no escape sequences are processed, so apostrophes and double quotes can both appear as is.
string path  = @"C:\Users\Sam's files";
string quote = @"She said ""it's done"".";
string wrong = @"It\'s";            // keeps the backslash: It\'s

string name  = "Sam";
string label = $"{name}'s file";     // Sam's file

string raw   = """It's "quoted" text""";

Using a typographic (curly) apostrophe

Typography usually renders an apostrophe as the right single quotation mark ’ (U+2019) rather than the straight ' (U+0027). Both characters work in C# strings without escaping. You have two ways to include the curly one:
Paste it directly into the source file. Keep the file saved in an encoding such as UTF-8 so the character survives.
Use the escape \u2019. This keeps the source file plain ASCII and makes the exact character visible to anyone reading the code.

string curly = "It\u2019s ready.";   // It’s ready.

Sometimes the console shows a question mark or garbled characters where the curly apostrophe should be. In that case, the string itself is often correct and the console output encoding is the problem. Setting Console.OutputEncoding = System.Text.Encoding.UTF8; before writing output is a common fix. Checking the code points, as described below, tells you which side is at fault.

Converting between straight and curly apostrophes

C# strings are immutable. string.Replace returns a new string and leaves the original untouched, so you can keep the original for comparison or recovery.
Converting curly apostrophes to straight ones is a simple one-to-one replacement. The reverse direction needs context. A straight ' can also serve as an opening single quote, and replacing every ' with ’ would turn those opening quotes into closing-style marks.

string original = "It’s Sam’s turn.";
string straight = original.Replace('\u2019', '\'');
// straight: It's Sam's turn.
// original is unchanged

Check the result

Looking at the output is not a reliable check, because ' and ’ look alike in many fonts. Print each character with its code point instead:

foreach (char c in "It’s")
    Console.WriteLine($"{c} U+{(int)c:X4}");

// Expected output:
// I U+0049
// t U+0074
// ’ U+2019
// s U+0073

Seeing U+0027 means you have the straight apostrophe, and U+2019 means the curly one. For a quick yes-or-no test in code, text.IndexOf(''') >= 0 reports whether a straight apostrophe is present.
The expected output in this article follows from the C# language rules, but the examples are illustrative. Run them in your own project to confirm the behavior with your compiler, runtime and console.

When the error isn't in C#

Sometimes a string compiles fine but fails later, for example when a name like O'Brien breaks a database query. In that case, the apostrophe is ending a string literal in the target language, not in C#.
For SQL, the fix is to use parameters instead of joining the value into the query text. Parameters also protect against SQL injection. For other output formats, apply that format's escaping rules at the point where the text is written, and leave the C# string itself unchanged.

using var command = new SqlCommand(
    "SELECT * FROM Users WHERE LastName = @lastName", connection);
command.Parameters.AddWithValue("@lastName", "O'Brien");