Skip to content
Learn Netverks

Lesson

Step 8/36 22% through track

strings-interpolation

Strings and interpolation

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Strings and interpolation: the syntax, patterns, and safety habits you need before advancing in C#.

Teams still ship Strings and interpolation in C# codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Strings and interpolation in contexts like: .NET services, Unity games, and Windows-centric tooling.

Write C# with Console.WriteLine (top-level or Program), click Run on server—the dev runner uses dotnet build/run on a temp net8 project (requires .NET SDK; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

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

  1. Q: Why are strings immutable?
    A: Thread safety, hash stability, and safe sharing—mutations create new instances.
  2. Q: $"" vs string.Format?
    A: Both compile similarly; interpolation is more readable for inline expressions.

Self-check

  1. What prefix makes a verbatim string?
  2. 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?

string is immutable—repeated concatenation allocates many interim strings; StringBuilder mutates a buffer for heavy building.

What is verbatim string syntax?

@\"C:\path\" avoids escaping backslashes—useful for file paths and multiline text.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • $"" vs string.Format?
  • StringBuilder when?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump