14. Modern C# Next#
Modern C# has evolved well beyond its Java-inspired origins. This chapter covers language features that make C# code more expressive, concise, and readable — without requiring a deep class hierarchy.
Why these features matter
These five features appear constantly in modern C# codebases:
Pattern matching — replaces chains of
if/elseandis/ascasts with clear, structural dispatchRecord types — eliminate boilerplate for data-carrying types; you have been using them since Ch.9 — now we name what they give you
Nullable operators — make null-handling explicit and safe;
?.,??,??=are in every real codebaseGenerics — explain the
<T>you have been using inList<T>and LINQ; writing generic methods and classes yourself reduces duplicationAsync/await — keep programs responsive while waiting for files, networks, or databases without writing complex threading code
Learning goals
By the end of this chapter, you should be able to:
use
switchexpressions andispatterns to match on type and structureexplain what
recordadds overclass: value equality,with, deconstruction, andinit-only propertieschoose between
record,class, andstructfor a given designhandle null safely using
?.,??,??=, and nullable annotationswrite and read generic methods and classes that use
<T>and type constraintscall
asyncmethods usingawaitand understand when asynchrony is appropriate