C# Special Characters in Strings

Special characters in strings can be represented using escape sequences, which start with a backslash (\). They are used to represent characters like newlines, tabs, or double quotes inside a string.

Key Topics

1. Common Escape Sequences

  • \n - Newline
  • \t - Tab
  • \" - Double quote
  • \\ - Backslash

Example: Using Escape Sequences

string message = "First Line\nSecond Line\tIndented";
Console.WriteLine(message);

2. Verbatim Strings

Verbatim strings start with @ and are used to ignore escape sequences, making it easier to write file paths and multi-line strings.

Example: Using Verbatim Strings

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

Key Takeaways

  • Escape sequences start with \ and represent special characters.
  • Use verbatim strings (@"") to simplify strings with backslashes.
  • Understanding escape sequences is essential for formatting strings correctly.