C# Escape

Convert text into a C# string literal, in either the regular or the verbatim form.

Text
Escaped
Runs locally in your browser

About the C# Escape

C# gives you two ways to write a literal and they need opposite treatment. A regular literal reads backslashes as escapes, so Windows paths turn into a wall of doubled slashes. A verbatim literal, written with a leading @, ignores backslashes entirely and only asks that an inner double quote be doubled. Switch the literal kind at the top and the output follows the matching rule, which is why a file path is usually far shorter in verbatim form.

In regular mode the full simple-escape set is used: \0, \a, \b, \f, \n, \r, \t, \v, the two quotes and the backslash. Remaining control characters become \uXXXX rather than \x, on purpose: the C# hex escape swallows between one and four digits, so \x9 immediately followed by the letter A silently becomes U+9A. The fixed-width Unicode form has no such ambiguity.

A null character followed by a digit is written as \u0000 for the same reason. Ticking the non-ASCII box escapes everything above the ASCII range, useful when a resource file or a code generator must stay in a single-byte encoding. C# Unescape reverses either mode, and JSON to C# builds classes when you need a model rather than a literal.

How to use

  1. Paste the value, for example a file path or a SQL query.
  2. Choose Regular or Verbatim depending on how the literal is written in your code.
  3. Tick Add surrounding quotes to get a complete literal, including the @ prefix in verbatim mode.
  4. Copy it into your C# file.

Common questions

When should I use verbatim mode?
For anything with many backslashes, such as Windows paths, regular expressions or a multi-line block, since only the double quote needs doubling there.
Why does the tool avoid \x escapes?
A C# \x escape takes a variable number of hex digits, so a following hex character can join the escape and change the value. The \u form is always four digits.
Does verbatim mode keep my line breaks?
Yes. A verbatim literal may span lines, so newlines are preserved instead of becoming \n.
What about raw string literals from C# 11?
Those need no escaping at all beyond choosing enough quote characters, so run the text through verbatim mode only if you are targeting an older language version.