Operators & Control Flow
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
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
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
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
?. (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.
| Feature | C (c1-3) | Java (java1-3) | C# |
|---|---|---|---|
| switch statement fallthrough | allowed by default | allowed by default | forbidden — compile error |
| switch expression arrival | n/a | Java 14 (2020) | C# 8 (2019) — first |
| Null-safe chaining operator | none | none — manual checks or Optional | ?. and ?? |
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.
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
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 solutionWrite 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 solutionWrite 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 solutionChapter 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