Skip to content
Learn Netverks

Lesson

Step 9/36 25% through track

control-flow-csharp

Control flow

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

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

Coroutines replace callback hell on Android and in Ktor—structured concurrency is interview-critical.

You will apply Control flow 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# control flow mirrors Java: if, else, for, while, do-while, and switch. C# also adds foreach over anything implementing IEnumerable—the foundation for LINQ later.

foreach loop

int[] nums = {1, 2, 3};
foreach (int n in nums) {
    Console.WriteLine(n);
}

switch expressions (preview)

string label = score switch {
    >= 90 => "A",
    >= 80 => "B",
    _ => "C"
};

Classic switch with case/break still works; modern switch expressions return values concisely.

Important interview questions and answers

  1. Q: foreach requirements?
    A: The collection must expose a public GetEnumerator() or be an array—works with lists, arrays, and LINQ results.
  2. Q: switch on strings?
    A: Supported directly—unlike older C++ where string dispatch needed maps or if/else chains.

Self-check

  1. How does do-while differ from while?
  2. What does the _ arm mean in a switch expression?

Tip: foreach works on any type with GetEnumerator()—same ergonomics as enhanced for-loops in Java.

Interview prep

switch expression vs statement?

Switch expressions return a value (x switch { ... }); switch statements execute blocks—both support pattern matching in modern C#.

foreach requirements?

The type needs a public GetEnumerator() returning something with MoveNext and Current.

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

  • switch expression?
  • foreach vs for?

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