Bird
Raised Fist0
C Sharp (C#)programming~5 mins

Verbatim and raw string literals in C Sharp (C#)

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Verbatim and raw string literals help you write text exactly as you want it, including new lines and special characters, without extra symbols or escapes.
When you want to write a file path without doubling backslashes, like C:\Users\Name.
When you need to include multi-line text exactly as it appears, such as a poem or a message.
When writing code that contains many quotes or special characters, and you want to avoid escaping them.
When you want to improve readability of strings that span multiple lines.
Syntax
C Sharp (C#)
Verbatim string literal:
string path = @"C:\Users\Name";

Raw string literal (C# 11+):
string text = """
This is a raw
multi-line string
with ""quotes"" and \backslashes\.
""";
Verbatim strings start with @ and use double quotes. Backslashes are treated as normal characters.
Raw string literals use triple quotes """ and preserve all formatting inside, including new lines and quotes.
Examples
Verbatim string lets you write Windows paths without doubling backslashes.
C Sharp (C#)
string filePath = @"C:\Program Files\App";
Verbatim strings keep new lines as written, so you can write multi-line text.
C Sharp (C#)
string multiLine = @"Line 1
Line 2";
Raw string literals keep all formatting and special characters exactly as typed.
C Sharp (C#)
string rawText = """
Hello ""World""!
Path: C:\Users\Name
""";
Sample Program
This program shows how verbatim and raw string literals print text exactly as written, including backslashes and quotes.
C Sharp (C#)
using System;

class Program
{
    static void Main()
    {
        string verbatim = @"C:\Users\Alice\Documents";
        string raw = """
This is a raw string literal.
It can contain ""quotes"" and \backslashes\ without escapes.
""";

        Console.WriteLine("Verbatim string output:");
        Console.WriteLine(verbatim);
        Console.WriteLine();
        Console.WriteLine("Raw string literal output:");
        Console.WriteLine(raw);
    }
}
OutputSuccess
Important Notes
Verbatim strings still require double quotes inside to be doubled, like "" for a double quote.
Raw string literals were introduced in C# 11, so make sure your project supports it.
Raw strings can use more than three quotes if your text contains triple quotes inside.
Summary
Verbatim strings start with @ and make writing paths and multi-line strings easier.
Raw string literals use triple quotes and preserve all formatting exactly as typed.
Both help avoid confusing escape sequences and improve code readability.

Practice

(1/5)
1. What is the main purpose of a verbatim string literal in C# (starting with @)?
easy
A. To write strings that ignore escape sequences and can span multiple lines easily.
B. To create strings that automatically convert to uppercase.
C. To define strings that are encrypted at compile time.
D. To declare strings that are immutable and cannot be changed.

Solution

  1. Step 1: Understand verbatim string syntax

    Verbatim strings start with @ and allow writing strings with backslashes and new lines without escape sequences.
  2. Step 2: Identify the purpose

    This makes writing file paths and multi-line text easier and more readable.
  3. Final Answer:

    To write strings that ignore escape sequences and can span multiple lines easily. -> Option A
  4. Quick Check:

    Verbatim strings ignore escapes = A [OK]
Hint: Verbatim strings start with @ and ignore escape sequences [OK]
Common Mistakes:
  • Thinking verbatim strings convert text case
  • Assuming verbatim strings encrypt content
  • Confusing immutability with verbatim syntax
2. Which of the following is the correct syntax for a raw string literal in C# 11+?
easy
A. """This is a raw string"""
B. @"This is a raw string"
C. 'This is a raw string'
D. "This is a raw string"

Solution

  1. Step 1: Recall raw string literal syntax

    Raw string literals in C# 11+ use triple double quotes to start and end the string.
  2. Step 2: Compare options

    """This is a raw string""" uses triple double quotes correctly; @"This is a raw string" is verbatim string syntax, not raw string.
  3. Final Answer:

    """This is a raw string""" -> Option A
  4. Quick Check:

    Raw strings use triple quotes = D [OK]
Hint: Raw strings use triple quotes """ at start and end [OK]
Common Mistakes:
  • Confusing verbatim strings (@) with raw strings (""")
  • Using single quotes for strings
  • Using normal double quotes for raw strings
3. What will be the output of this C# code?
string path = @"C:\Users\Admin";
string raw = """C:\Users\Admin""";
Console.WriteLine(path);
Console.WriteLine(raw);
medium
A. C:\\Users\\Admin C:\\Users\\Admin
B. C:\Users\Admin C:\Users\Admin
C. C:UsersAdmin C:UsersAdmin
D. C:\UsersAdmin C:\Users\Admin

Solution

  1. Step 1: Understand verbatim string output

    The verbatim string @"C:\Users\Admin" outputs the path with single backslashes because escapes are ignored.
  2. Step 2: Understand raw string output

    The raw string """C:\Users\Admin""" preserves the backslashes exactly as typed, so output is the same.
  3. Final Answer:

    C:\Users\Admin C:\Users\Admin -> Option B
  4. Quick Check:

    Both print path with single backslashes = B [OK]
Hint: Both verbatim and raw strings preserve backslashes as typed [OK]
Common Mistakes:
  • Expecting double backslashes in output
  • Confusing escape sequences in verbatim strings
  • Thinking raw strings remove backslashes
4. Identify the error in this code snippet:
string text = @"This is a "quoted" word.";
medium
A. Raw string literals require triple quotes, not @.
B. Verbatim strings cannot contain double quotes.
C. No error, code is correct.
D. Missing escape for inner quotes inside verbatim string.

Solution

  1. Step 1: Check verbatim string rules for quotes

    In verbatim strings, double quotes inside must be doubled to escape them ("" for one ").
  2. Step 2: Identify the error

    The code uses single double quotes inside verbatim string, causing syntax error.
  3. Final Answer:

    Missing escape for inner quotes inside verbatim string. -> Option D
  4. Quick Check:

    Double quotes inside verbatim need doubling = C [OK]
Hint: Double inner quotes "" inside verbatim strings to escape [OK]
Common Mistakes:
  • Using single double quotes inside verbatim strings
  • Confusing verbatim and raw string syntax
  • Assuming verbatim strings allow unescaped quotes
5. You want to store this multi-line JSON string exactly as shown, including indentation and quotes, in C# 11+. Which is the best way to do it?
{
  "name": "Alice",
  "age": 30
}
hard
A. Use a normal string with \n for new lines and escaped quotes.
B. Use a verbatim string with @ and escape all quotes with backslashes.
C. Use a raw string literal with triple quotes preserving all formatting.
D. Use string concatenation for each line.

Solution

  1. Step 1: Understand formatting needs

    The JSON string has multiple lines, indentation, and quotes that must be preserved exactly.
  2. Step 2: Choose best string literal

    Raw string literals with triple quotes preserve all formatting and quotes without escapes, making code clean and readable.
  3. Final Answer:

    Use a raw string literal with triple quotes preserving all formatting. -> Option C
  4. Quick Check:

    Raw strings preserve multi-line and quotes = A [OK]
Hint: Use raw strings for exact multi-line text with quotes [OK]
Common Mistakes:
  • Escaping quotes manually in verbatim strings
  • Using normal strings with many escapes
  • Concatenating strings unnecessarily