πŸ“Œ1. Escape Sequences (inside strings/char literals)

C# uses the backslash (\) to introduce escape sequences.

EscapeMeaningExampleOutput
\'Single quoteConsole.WriteLine('\'');'
\"Double quoteConsole.WriteLine("\"");"
\\BackslashConsole.WriteLine("\\");\
\0Null char"A\0B".Length β†’ 3A␀B
\aAlert (beep)Console.Write("\a");(beep sound)
\bBackspace"AB\bC"AC
\fForm feedRarely used (page break)–
\nNewline (LF)"Hello\nWorld"Hello ↡ World
\rCarriage return (CR)"Hello\rWorld"Worldo
\tTab"A\tB"A B
\vVertical tabRare, whitespace–
\uXXXXUnicode (16-bit)\u03A9Ξ©
\UXXXXXXXXUnicode (32-bit)\U0001F600πŸ˜€
\xNNHex (variable length)\x41A

πŸ”Ή Example:

string demo = "Line1\nLine2\tTabbed";
Console.WriteLine(demo);

πŸ“Œ2. Verbatim Strings (@"")

Use @ before a string to treat backslashes literally and allow multiline text.

string path = @"C:\Users\Tyson\Docs\file.txt";
string multi = @"Line1
Line2
Line3";

⚠️ To include a quote in a verbatim string: ""

string quote = @"She said, ""Hello!""";

πŸ“Œ3. Interpolated Strings ($"")

Use $ to embed variables/expressions with {}:

int age = 25;
string name = "Tyson";
string msg = $"Hello, {name}. You are {age} years old.";

βœ… You can combine with @ for multi-line interpolated strings:

string folder = "Docs";
string path = $@"C:\Users\Tyson\{folder}\file.txt";

πŸ“Œ4. Special Characters in Identifiers

Normally you cannot name a variable the same as a keyword, but you can prefix it with @.

int @class = 10;   // allowed, but not recommended
Console.WriteLine(@class);

This is useful when working with reserved words (e.g., mapping DB fields).


πŸ“Œ5. Digit Separators & Discards (_)

  • _ can be used as a digit separator in numbers:

    int million = 1_000_000;
    double pi = 3.14_15_92;
  • _ can be used as a discard when you don’t care about a variable:

    var (_, lastName) = ("John", "Doe"); // ignore first value

  • Nullable types:

    int? maybe = null;
  • Null-conditional:

    person?.SayHello();
  • Null-coalescing:

    string result = name ?? "Unknown";
  • Null-coalescing assignment:

    name ??= "Default";

πŸ“Œ7. Other Special Syntax Characters

SymbolMeaning
;Statement terminator
{ }Code blocks, object initializers
( )Method calls, grouping
[ ]Arrays, attributes, indexers
,Separates items in lists
.Member access
:Used in ternary, inheritance, case labels
? :Ternary conditional operator
=>Lambda expressions
->Function pointers (C# 9+)
#Preprocessor directives (#define, #region, etc.)
$Interpolated strings
@Verbatim strings, identifiers
_Digit separator, discard

πŸ“Œ8. Preprocessor Special Characters (#)

Lines starting with # are compiler instructions:

#define DEBUG
#if DEBUG
Console.WriteLine("Debug mode");
#endif
 
#region Helpers
// ...
#endregion

πŸ“Œ9. Regex & Escaping Gotchas

When writing regex or Windows paths, escaping can get messy.
Best practice β†’ use @"" verbatim strings.

string pattern = @"\d{3}-\d{2}-\d{4}";

Instead of:

string pattern = "\\d{3}-\\d{2}-\\d{4}";

πŸ“Œ10. Summary

  • Use \ escape sequences for control characters.

  • Use @ for verbatim strings (literal backslashes, multiline).

  • Use $ for interpolation.

  • Use @ before identifiers to allow keywords.

  • Watch for null operators (?, ??, ??=).

  • Learn common syntax symbols (;, {}, =>, #).