In this chapter, the author builds a practical bridge between the mathematical idea of a function and the ways functions are expressed in C#. A mathematical function is described as a mapping from a domain to a codomain, where the output depends only on the input. In C#, types stand in for those sets, and a function’s type or signature acts like a contract that states what inputs it accepts and what outputs it produces. This perspective helps establish the functional mindset used throughout the book, while also acknowledging that programming functions can sometimes depend on more than just their explicit arguments.
The chapter then surveys the main C# constructs used to represent functions: methods, delegates, lambda expressions, and dictionaries. Static methods are presented as the most familiar and idiomatic choice, especially when they behave like mathematical functions. Delegates are introduced as type-safe function pointers that let functions be treated as values, with the built-in Func and Action families providing flexible, reusable types for many common signatures. Lambdas offer a concise way to define functions inline, and closures allow them to capture variables from the surrounding scope. Dictionaries are also reframed as function-like mappings, which is useful both for representing arbitrary associations and for techniques such as memoization.
From there, the chapter turns to higher-order functions, emphasizing their power to take functions as inputs, return functions as outputs, or do both. Examples such as Where and Sort show how HOFs can delegate decision-making to the caller, enabling separation of concerns and flexible reuse. The chapter also describes adapter functions, such as swapping argument order, and function factories, such as building predicates from parameters. A final example shows how HOFs can remove duplication by wrapping common setup and teardown logic around database operations, making the code shorter and clearer while preserving flexibility. The chapter closes by encouraging careful use of these abstractions so that functional power does not come at the expense of readability.
A mathematical function is simply a mapping. It maps elements from one set (the domain) to elements of another set (the codomain).
Where is a HOF that takes a predicate function as input. It uses the predicate to decide which elements to include in the returned list.
A HOF that iteratively applies the function given as an argument
A HOF that conditionally applies the function given as an argument
Here we define IsMod as a HOF that returns a function telling us whether a number is divisible by some given value. We then take the resulting predicate and use it as input to Where.
A HOF that wraps a given function between setup and teardown logic
Summary
- Mathematical functions simply define a mapping between two sets.
- You can represent functions in C# using methods, delegates, lambdas, and dictionaries.
- FP leverages higher-order functions (HOFs), which are functions that take other functions as input or output); hence, the necessity for the language to have functions as first-class values.
Functional Programming in C#, Second Edition ebook for free