Leanpub Header

Skip to main content

Keeping Up with C#

From C# 5 to C# 14: A Thematic Journey Through Language Evolution

This book is 100% completeLast updated on 2026-07-21

Ninety-two C# features, from C# 5 to C# 14, organized by the problems they solve — with verbatim code from five runnable projects and seven production-ready patterns, all on .NET 10 LTS.

Minimum price

Free!

$19.99

You pay

Author earns

$
PDF
Discussion Forum
About

About

About the Book

C# is a different language than it was a decade ago.

Since async/await landed in C# 5, the language has shipped ninety-two features: records, pattern matching, nullable reference types, Span<T>, source generators, primary constructors, collection expressions, extension members, the field keyword. Each one solves a specific problem. Together, they've changed how modern C# is written, read, and debugged.

Keeping Up with C# is a thematic tour of those ninety-two features. Not a reference — the Microsoft docs are the reference, and they're good. This is the book that walks you through what changed, why it changed, and how the features compose into patterns you can actually reach for in production code.

Instead of marching version by version, chapters group features by the problem they solve: expressiveness, data modeling, pattern matching, type-system flexibility, safety, memory and allocation control, native interop, async, and compiler tooling. The capstone chapter then assembles everything into seven composite patterns — Immutable Data, Null-Safe API, Zero-Allocation, Scripting, Source Generator, Generic Math, and Async Pipeline — the shapes modern C# codebases are actually built from.

Every code snippet is quoted verbatim from one of five MIT-licensed companion projects: QuantLite (a financial trade modeler), DevScripts (a developer toolbox with a working Roslyn source generator), ChatStream (a real-time messaging pipeline), TypeForge (a type-system playground), and PerfBench (a benchmarking harness). Clone any one of them and dotnet run works. Each chapter ends with practice exercises, and Appendix C walks through every solution.

The book is written for three readers: the developer who learned C# 5 or 6 and feels the language moved on without them; the developer arriving from Java, Go, TypeScript, or Rust who needs to know what's idiomatic and what's legacy; and the team lead deciding what to adopt in an existing codebase — Appendix A, the Migration & Adoption Guide, covers LangVersion strategy, nullable reference types rollout, .editorconfig enforcement, and a four-tier adoption priority matrix.

What this book is not: a beginner's guide. It assumes you read C# 5 fluently and nothing past C# 7.

Everything targets C# 14 on .NET 10 LTS (supported until November 2028). All you need is the .NET 10 SDK and any editor.

Author

About the Author

Rachid DAHIR

I design systems for the long haul.

As a software architect, technical author, and educator based in Morocco, I've spent more than a decade building enterprise-grade .NET solutions in financial services, healthcare, and large-scale enterprise environments — where reliability, performance, and clean architecture are not optional.

I write for developers who are ready to move past tutorials. My books share a deliberate standard: production-grade code only — no toy examples, no shortcuts that would never survive a code review; every concept explained, demonstrated, then practiced; every advanced topic taught with the same patience as an introductory one — because complexity is never an excuse for a poor explanation.

Whether you're a mid-career developer leveling up, a solution architect consolidating best practices, or an enterprise team standardizing modern .NET, my goal stays the same: clarity without compromise.

When I'm not writing code or prose, I explore mathematics, AI research, and natural health.

Contents

Table of Contents

Table of Contents

  • Preface
  • Acknowledgements
  • Introduction
  • About the Author
  1. The Foundation (C# 1–4)
    • Why revisit the foundation?
      • Features in this chapter
    • Generics
      • Variance
    • Nullable value types
    • Iterators and yield return
    • LINQ and its dependencies
      • Extension methods
      • Lambda expressions (and a nod to anonymous methods)
      • LINQ itself
      • Expression trees
    • Everyday conveniences
      • var
      • Object and collection initializers
      • Anonymous types
      • Auto-implemented properties
    • Named and optional parameters
    • Dynamic binding
    • Summary
    • Key Terms
    • Practice Exercises
    • What's Next
  2. Expressiveness & Boilerplate Reduction
    • What this chapter is really about
      • Features in this chapter
    • Shrinking the top of the file
      • File-scoped namespaces
      • Global and implicit usings
      • Top-level statements and file-based apps
    • Strings, reimagined
      • Basic interpolation through raw strings
      • Constant interpolated strings
    • Numeric and default literals
      • Digit separators and binary literals
      • The default literal
    • Member concision
      • Expression-bodied members
      • Local and static local functions
    • Object construction
      • Target-typed new
      • Primary constructors
    • Collections and variadic arguments
      • Collection expressions
      • params enhancements
    • The field keyword
    • Summary
    • Key Terms
    • Practice Exercises
    • What's Next
  3. Data Modeling & Functional Techniques
    • Data as values
      • Features in this chapter
    • Tuples and discards
      • Tuples
      • Discards
    • Records — the crown jewel
      • record class vs record struct vs readonly record struct
    • Init-only properties
    • Required members
    • With-expressions
    • Readonly members on structs
    • Summary
    • Key Terms
    • Practice Exercises
    • What's Next
  4. Evolution of Control Flow (Pattern Matching)
    • Why pattern matching mattered
      • Features in this chapter
    • Type patterns
    • Property patterns
    • Switch expressions
    • Relational and logical patterns
    • List patterns
    • Summary
    • Key Terms
    • Practice Exercises
    • What's Next
  5. Type System & OOP Flexibility
    • Why the type system kept growing
      • Features in this chapter
    • Default interface methods
    • Static abstract members and generic math
    • Generic constraints
    • Ref struct interfaces
    • Extension members
    • Small additions that matter
    • Summary
    • Key Terms
    • Practice Exercises
    • What's Next
  6. Safety & Robustness
    • What the compiler started catching for you
      • Features in this chapter
    • Nullable reference types
    • The null-conditional family
      • ?. and ?[]
      • ??=
      • ?.=
    • Exception filters
    • CallerArgumentExpression
    • Checked user-defined operators
    • System.Threading.Lock
    • Summary
    • Key Terms
    • Practice Exercises
    • What's Next
  7. Memory & Allocation Control
    • Why allocation became a language feature
      • Features in this chapter
    • ref returns, ref locals, ref readonly
    • Span<T> — the core abstraction
    • ref struct and the rules around them
    • stackalloc in safe contexts
    • Memory<T> for async-compatible buffers
    • ArrayPool<T> and MemoryPool<T>
    • Ranges and indices
    • UTF-8 string literals
    • allows ref struct and the C# 14 papercut fixes
    • Summary
    • Key Terms
    • Practice Exercises
    • What's Next
  8. Low-Level & Interop Primitives
    • The last layer between managed and native
      • Features in this chapter
    • Native integers — nint and nuint
    • Function pointers — delegate*
    • Unmanaged constructed types
    • [StructLayout] for binary protocols
    • [LibraryImport] — modern P/Invoke
    • Summary
    • Key Terms
    • Practice Exercises
    • What's Next
  9. Asynchronous Programming Evolution
    • Why async is hard and why C# fixed it
      • Features in this chapter
    • async/await foundation
    • The async state machine
      • When to elide async
    • ValueTask<T> and when to use it
    • Async streams — IAsyncEnumerable<T>
    • IAsyncDisposable
    • ConfigureAwait and SynchronizationContext
    • Cancellation patterns
      • Timeouts
      • Linked tokens
      • Registration callbacks
    • Task combinators
      • Task.WhenAll
      • Task.WhenAny
      • Task.WhenEach (.NET 9)
    • Summary
    • Key Terms
    • Practice Exercises
    • What's Next
  10. Compiler & Tooling Integration
    • The meta-layer
      • Features in this chapter
    • Caller info attributes
    • Module initializers
    • Source generators
      • The generator project
      • The incremental generator pipeline
      • The output
    • Interceptors
    • Partial members: properties, constructors, events
      • Partial properties (C# 13)
      • Partial constructors (C# 14)
      • Partial events (C# 14)
    • Summary
    • Key Terms
    • Practice Exercises
    • What's Next
  11. Modern C# in Practice — Capstone (7 Patterns)
    • Why these patterns
      • Patterns in this chapter
    • Pattern 1: Immutable Data
    • Pattern 2: Null-Safe API
    • Pattern 3: Zero-Allocation
    • Pattern 4: Scripting
    • Pattern 5: Source Generator
    • Pattern 6: Generic Math
    • Pattern 7: Async Pipeline
    • Closing — what you've built up
    • Summary
    • Key Terms
    • Practice Exercises (final)
    • The end
  • Appendix A — Migration & Adoption Guide
    • 1. LangVersion vs. TargetFramework
      • Per-project overrides
      • When to bump TargetFramework
    • 2. Enabling Nullable Reference Types Incrementally
      • Strategy A: File by file (for small teams or solo projects)
      • Strategy B: Project by project (for mid-size teams)
      • Strategy C: annotations-only first, then warnings (for large codebases)
      • Common warnings and how to handle them
      • Reflection-based libraries
    • 3. Using .editorconfig to Enforce Modern Idioms
      • Integrating with CI
    • 4. Adoption Priority Matrix
      • Tier 1 — Immediate (adopt today, no excuses)
      • Tier 2 — Next Sprint (plan, pilot, roll out)
      • Tier 3 — Planned (next quarter, specific scenarios)
      • Tier 4 — When Needed (specific demand, not speculative)
      • Adoption audit checklist
    • Closing
  • Appendix B — Feature Quick-Reference Index
    • Alphabetical Feature List
    • Legend
    • How to use this index
  • Appendix C — Exercise Solutions
    • Chapter 1 — Answers
      • Basic — TradeRepository<T>.Remove
      • Intermediate — Top-3 tickers by total notional
    • Chapter 2 — Answers
      • Basic — Primary-constructor rewrite of OldConfigService
      • Intermediate — [Flags] FilePermissions + Describe helper
    • Chapter 3 — Answers
      • Basic — OldTrade → record CompactTrade
      • Intermediate — VersionedTrade.Amend()
    • Chapter 4 — Answers
      • Basic — switch-statement ClassifyRisk → switch expression
      • Intermediate — CalculateDiscount with relational patterns
    • Chapter 5 — Answers
      • Basic — Default interface methods with virtual dispatch
      • Intermediate — Generic Median<T> with INumber<T>
    • Chapter 6 — Answers
      • Basic — ChatConfigOld rewritten with ??=
      • Intermediate — Guard.Against.MatchesRegex
    • Chapter 7 — Answers
      • Basic — Stackalloc/ArrayPool rewrite of ProcessSensorBatchHeap
      • Intermediate — Zero-allocation IPv4 parser
    • Chapter 8 — Answers
      • Basic — [DllImport] → [LibraryImport] conversion
      • Intermediate — PacketHeaderV2 with checksum at offset 14
    • Chapter 9 — Answers
      • Basic — Task<T> → ValueTask<T> on cache hit
      • Intermediate — Task.WhenEach progress indicator
    • Chapter 10 — Answers
      • Basic — Timed() with caller-info attributes
      • Intermediate — [AutoEquals] generator shape
    • Chapter 11 — Answers
      • Basic — Immutable Data Pattern on a DTO
      • Intermediate — Source Generator Pattern (reflection → switch)
    • Running the answers
  • Index

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub