Overview

1 Introducing functional programming

Functional programming is introduced as a different way of thinking about software, one that emphasizes treating functions as values and avoiding state mutation. The chapter frames this shift as both challenging and rewarding, encouraging readers to experiment in a REPL so they can see functional ideas in action. It also presents the core motivation for the book: learning to reason about code in a more declarative, composable, and predictable way.

The chapter explains that C# supports many functional techniques well, especially through delegates, lambda expressions, LINQ, tuples, records, and pattern matching. LINQ is highlighted as a strongly functional part of the language because it uses functions as arguments and returns new sequences instead of modifying existing ones. Records and pattern matching are then shown as especially useful for type-driven design, allowing concise immutable data models and clear branching logic without the boilerplate often associated with object-oriented approaches.

A major theme is that avoiding mutation improves reliability, especially under concurrency, because multiple processes can safely read shared data when it is not being changed in place. The chapter contrasts this with imperative code that can create unpredictable results when state is modified during parallel execution. It closes by positioning functional programming as a practical toolkit for writing cleaner, more testable, and more modular C# code, and it previews the broader goals of the book: using functional techniques to build expressive APIs, manage complexity, and structure programs around composition and pure behavior.

Modifying data in place can give concurrent threads an incorrect view of the data.
The functional approach: creating a new, modified version of the original structure

Summary

  • Functional programming (FP) is a powerful paradigm that can help you make your code more concise, maintainable, expressive, robust, testable, and concurrency-friendly.
  • FP differs from object-oriented programming (OOP) by focusing on functions rather than objects, and on data transformations rather than state mutations.
  • FP can be seen as a collection of techniques that are based on two fundamental tenets:
    • Functions are first-class values.
    • In-place updates should be avoided.
  • C# is a multi-paradigm language that has steadily incorporated functional features, allowing you to reap the benefits of programming in a functional style.

FAQ

What is functional programming?Functional programming is a programming paradigm that emphasizes functions while avoiding state mutation. In this chapter, it is presented as a style built around two main ideas: treating functions as first-class values and avoiding in-place updates to data.
What does it mean for functions to be first-class values in C#?It means functions can be treated like any other value: you can store them in variables, pass them as arguments, return them from other functions, and place them in collections. C# supports this with delegates and lambda expressions.
Why is avoiding state mutation important in functional programming?Avoiding state mutation helps prevent bugs caused by values changing in place. It also makes programs easier to reason about, test, and use safely in concurrent scenarios because data is not unexpectedly altered.
How do LINQ methods like Select, Where, and OrderBy support functional programming?They are functional because they take functions as arguments and return new sequences rather than modifying the original data. Select maps, Where filters, and OrderBy sorts by creating new results.
Why is in-place sorting considered nonfunctional?In-place sorting changes the original array or list directly, destroying the original ordering. That kind of destructive update is contrary to the functional principle of preserving existing data and returning new values instead.
How does functional programming help with concurrency?When data is immutable, multiple threads can read it safely without interfering with one another. This reduces race conditions and makes concurrent code more predictable than code that mutates shared state.
Is C# a functional language?C# is not purely functional, but it has strong support for functional programming techniques. It supports first-class functions, LINQ, tuples, records, pattern matching, expression-bodied members, and other features that make functional style practical.
What C# features are especially useful for functional programming?Key features include lambda expressions, delegates, LINQ, using static, expression-bodied members, local functions, tuples, records, and pattern matching. These features help make functional code shorter, clearer, and more expressive.
How do records and pattern matching help with type-driven programming?Records provide concise immutable data types, and pattern matching lets you choose behavior based on the shape or type of data. Together, they make it easy to write declarative, type-driven code such as the VAT calculation example.
What will readers learn from the rest of the book?Readers will learn how to use higher-order functions, pure functions, and functional APIs to write modular, testable, declarative code. The book also covers handling nullability and validation, building functional web APIs, and working effectively with functional-language ideas in C#.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free