Modern C# provides several types for working with memory directly while avoiding garbage collector pressure. Span
Strings are immutable in C#, so every modification creates a new allocation. This makes StringBuilder the right tool when building strings in loops or concatenating many pieces, with O(1) amortized appends. String interpolation (C# 6) embeds expressions in a string with $ and {} placeholders, with the compiler emitting a string.Format call or, in .NET 6+, a DefaultInterpolatedStringHandler for better performance. Verbatim strings (prefix @) disable escape processing and allow multi-line text, with "" for embedded quotes, and they combine with $ for interpolated verbatim strings. Raw string literals (C# 11) delimit multi-line text with three or more double quotes, allowing embedded quotes freely and using indentation rules based on the closing quote position. When the final length is known, string.Create(length, state, action) is the fastest way to format a string, since it writes directly into the backing Span
String interning stores one copy of each distinct literal in a global pool, with string.Intern and string.IsInterned for runtime values; interned strings are reference-equal, which can be useful for memory optimization but is risky for unbounded or untrusted data. StringComparison.Ordinal compares raw code units, is culture-independent and fast, and is appropriate for internal identifiers, paths, and security checks; StringComparison.CurrentCulture uses the current culture's collation rules and suits user-visible text. Similarly, StringComparer.Ordinal is the right choice for hash-based collections of identifiers, while StringComparer.InvariantCulture applies the invariant culture's rules for cross-machine consistency. The System.Text.Encoding class converts between chars and bytes: Encoding.UTF8 is the safe default, while Encoding.Default uses the system's ANSI code page and should be avoided in cross-platform code. For null checks, string.IsNullOrEmpty covers null and empty strings, while string.IsNullOrWhiteSpace also treats whitespace-only strings as null. The Convert class uses banker's rounding on doubles and tolerates null input, while an explicit cast on a double truncates toward zero. Boxing and unboxing remain expensive in hot paths, which is why generics like List