C# string is immutable—methods like Replace return new strings. Concatenation uses +, but string interpolation ($"...") is the idiomatic way to embed expressions, cleaner than printf in C or + chains in older Java.
Interpolation and verbatim strings
string name = "Ada";
string msg = $"Hello, {name}!";
string path = @"C:\logs\app.txt"; // verbatim: backslashes literal
Interpolation evaluates expressions inside {...}. Use @ before the quote for paths and multiline text without escaping backslashes.
Common string APIs
"hello".Length;
"hello".ToUpper();
string.IsNullOrWhiteSpace(text);
Prefer StringComparison.OrdinalIgnoreCase for culture-invariant comparisons in code paths, not user-facing sort rules.
Important interview questions and answers
- Q: Why are strings immutable?
A: Thread safety, hash stability, and safe sharing—mutations create new instances. - Q: $"" vs string.Format?
A: Both compile similarly; interpolation is more readable for inline expressions.
Self-check
- What prefix makes a verbatim string?
- Does ToUpper mutate the original string?
Tip: Prefer $"Hello, {name}" over + concatenation—strings are immutable and repeated concat allocates many interim objects.
Interview prep
- string vs StringBuilder?
stringis immutable—repeated concatenation allocates many interim strings;StringBuildermutates a buffer for heavy building.- What is verbatim string syntax?
@\"C:\path\"avoids escaping backslashes—useful for file paths and multiline text.