C# Verbatim Strings and Escape Sequences

Verbatim strings in C# are prefixed with @ and are used to create strings that ignore escape sequences. This is particularly useful for file paths and multi-line strings.

Key Topics

1. Using Verbatim Strings

Example: File Paths

string filePath = @"C:\Users\John\Documents\file.txt";
Console.WriteLine(filePath); // Outputs: C:\Users\John\Documents\file.txt

2. Multi-line Strings

Example: Creating Multi-line Strings

string multiLine = @"This is a multi-line string.
It spans multiple lines.
Tabs and spaces are preserved.";
Console.WriteLine(multiLine);

3. Including Double Quotes

To include double quotes in a verbatim string, use double double-quotes.

Example: Double Quotes in Verbatim Strings

string quote = @"She said, ""Hello!"" and waved.";
Console.WriteLine(quote); // Outputs: She said, "Hello!" and waved.

Key Takeaways

  • Verbatim strings ignore escape sequences and preserve formatting.
  • Useful for file paths and multi-line strings.
  • Use double double-quotes to include a double quote in the string.