Control Flow and Methods in C#
Controlling a program's flow comes down to three questions: which code runs (conditions and switch), how many times (loops), and how to split the work into named pieces (methods). The C# syntax here resembles C, Java and JavaScript, so the bugs appear exactly where the rules differ.
The C#-specific traps: a condition is bool only, so if (x = 0) does not compile; the switch statement does not let a section silently run into the next one, while the switch expression returns a value and throws when no arm matches; a foreach variable cannot be assigned; break and continue act on one level and take no labels; functions in a top-level program are local and cannot be overloaded. The layer-by-layer breakdown is below.
Topic map
- Conditionals —
if/else if/else, blocks and the stray;trap, the?:operator,switchexpression basics. - The switch statement —
case,breakanddefault, no implicit fall-through (CS0163), several labels per section,goto case,switchon strings. - Loops —
while,do/while,for,foreach, infinite loops, the read-only iteration variable (CS1656), off-by-one errors. - break and continue — leaving early and skipping an iteration, nested loops, leaving both loops with
return. - Methods — local functions versus class methods, parameters and arguments,
returnandvoid, optional and named arguments,ref/out/in, recursion.
Common Mistakes and Traps
| Mistake | Consequence |
|---|---|
Writing if (count) or if (x = 0) | Error CS0029: a condition must be a bool; a number or an assignment does not qualify |
Putting ; after if (…) | The body becomes an empty statement and the next block always runs; the compiler only warns with CS0642 |
Forgetting break in a switch section | Error CS0163: C# has no implicit fall-through into the next section |
Expecting break in a switch to leave the loop | break ends only the switch; the loop keeps going |
Assigning to a foreach variable | Error CS1656: it is read-only; change elements through an index in a for |
Removing List<T> elements inside a foreach | InvalidOperationException: a collection cannot change during enumeration |
Placing continue in a while before the step | The step never runs and the loop spins forever |
Expecting a method to change a passed int | Arguments are passed by value; only ref or out can change the caller's variable |
Interview relevance
In an interview these topics come as tasks: walk an array, find an element, work out the output of nested loops, explain what a switch prints. The interviewer watches whether you write loop bounds without off-by-one errors and know the C# rules that set it apart from C and Java.
Typical checks:
- The difference between
whileanddo/while,forandforeach, and when to use each. - Why C# has no implicit fall-through between
switchsections and how to group labels. - How the
switchstatement differs from theswitchexpression. - How to leave two nested loops at once.
- The difference between passing by value,
refandout, and whyint.TryParseusesout. - What optional and named arguments are, and the danger of recursion without a base case.
Common wrong answer: "break inside a switch stops the loop the switch sits in." That opens the discussion that break acts on the nearest enclosing construct — the switch itself — and that leaving the loop takes return, a flag or goto, because break in C# takes no label.