π1. Escape Sequences (inside strings/char literals)
C# uses the backslash (\
) to introduce escape sequences.
Escape | Meaning | Example | Output |
---|---|---|---|
\' | Single quote | Console.WriteLine('\''); | ' |
\" | Double quote | Console.WriteLine("\""); | " |
\\ | Backslash | Console.WriteLine("\\"); | \ |
\0 | Null char | "A\0B".Length β 3 | AβB |
\a | Alert (beep) | Console.Write("\a"); | (beep sound) |
\b | Backspace | "AB\bC" | AC |
\f | Form feed | Rarely used (page break) | β |
\n | Newline (LF) | "Hello\nWorld" | Hello β΅ World |
\r | Carriage return (CR) | "Hello\rWorld" | Worldo |
\t | Tab | "A\tB" | A B |
\v | Vertical tab | Rare, whitespace | β |
\uXXXX | Unicode (16-bit) | \u03A9 | Ξ© |
\UXXXXXXXX | Unicode (32-bit) | \U0001F600 | π |
\xNN | Hex (variable length) | \x41 | A |
πΉ 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
π6. Null-related Operators (?
)
-
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
Symbol | Meaning |
---|---|
; | 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 (
;
,{}
,=>
,#
).