Operators & Control Flow

Course 1 · Ch 3
Operators & Control Flow
Fallthrough forbidden by default — the opposite of C and Java's own switch default

Most of C#'s operators are shared syntax with every C-family language already covered on this site. The genuine differences are concentrated in one place: switch, and two operators Java has no direct equivalent for at all.

Standard Operators & Integer Division

Arithmetic, relational, and logical operators behave exactly as in C/C++/Java. Integer division still truncates toward zero — the same behavior c1-3 and java1-3 already covered, inherited unchanged.

switch Statements — Fallthrough Forbidden by Default

switch (day) { case 1: Console.WriteLine("Monday"); // no break here — this is a COMPILE ERROR in C#, not a silent bug case 2: Console.WriteLine("Tuesday"); break; }

This is a genuine, real difference worth stating plainly: C#'s switch statement forbids implicit fallthrough between non-empty case blocks — the exact opposite default of c1-3's C and java1-3's Java. A case with a statement body must end in break, return, throw, or an explicit goto case — omitting all four is a compile error, not a bug waiting to happen at runtime. Stacked empty case labels (case 1: case 2: DoSomething(); break;) are still fine, since neither label has its own body to fall out of.

switch Expressions — Pattern Matching Since Day One

string result = day switch { 1 or 2 or 3 => "Early week", 6 or 7 => "Weekend", _ => "Other" // the discard pattern — C#'s default case };

C# 8 (2019) shipped switch expressions with real pattern matching already built in — genuinely before java1-3's own arrow-based switch expressions landed in Java 14 (2020). Another "C# arrived first" moment, alongside Course 2's own records chapter.

Pattern Matching in switch Expressions

string Describe(object obj) => obj switch { int n when n > 0 => "a positive int", // type pattern + relational guard string s when s.Length == 0 => "an empty string", null => "null", _ => "something else" };

Type patterns, relational guards via when, and a dedicated null pattern were all present from C# 8's initial launch — a notably richer starting point than java1-3's own simpler initial arrow-only form.

Null-Conditional and Null-Coalescing Operators

string city = customer?.Address?.City; // ?. — short-circuits to null the instant anything is null string display = city ?? "Unknown"; // ?? — supplies a default only if the left side is null

?. (null-conditional) short-circuits an entire chain to null the moment any link is null, instead of throwing. ?? (null-coalescing) supplies a fallback value only when the left-hand side is null. Neither operator has a direct Java equivalent — Java requires either manual null checks or an Optional-based rewrite to express the same idea.

FeatureC (c1-3)Java (java1-3)C#
switch statement fallthroughallowed by defaultallowed by defaultforbidden — compile error
switch expression arrivaln/aJava 14 (2020)C# 8 (2019) — first
Null-safe chaining operatornonenone — manual checks or Optional?. and ??
Chain ?. and ?? together for concise null-safe reads
customer?.Address?.City ?? "Unknown" reads as one concise, self-documenting expression — short-circuit through any null link, then supply a fallback — replacing several lines of nested null checks in one line.
C# catches the missing-break bug at compile time, not runtime
Where c1-3 and java1-3 both warned that a missing break silently falls through into the next case, C#'s compiler simply refuses to build code shaped like that in the first place — a real, structural safety improvement over both languages' own default.

Coding Challenges

Challenge 1

Write a switch statement over an int month (1-12) with a case that deliberately omits a break after a non-empty body. Show the resulting compile error, then fix it.

📄 View solution
Challenge 2

Write a switch expression over an object using type patterns to distinguish int, string, and null, each returning a different descriptive string, with a discard pattern as the final case.

📄 View solution
Challenge 3

Write a class hierarchy of at least two levels deep (e.g. a Customer with a nullable Address property, which itself has a nullable City property), and use ?. chained with ?? to safely read the city with a default fallback, without any explicit null checks.

📄 View solution

Chapter 3 Quick Reference

  • switch statements forbid implicit fallthrough between non-empty cases — a compile error, the opposite default of C (c1-3) and Java (java1-3)
  • switch expressions with pattern matching shipped in C# 8 (2019), before Java's own arrow-based version in Java 14 (2020)
  • Type patterns, relational when guards, and a null pattern were all present in C#'s switch expressions from the start
  • ?. (null-conditional) and ?? (null-coalescing) have no direct Java equivalent — chain them for concise null-safe reads
  • Next chapter: classes and objects — auto-implemented properties as a first-class language feature