Chapter 3. Functional Coding in C# 7 and Beyond

I’m not sure when exactly the decision was made to make C# a hybrid object-oriented/functional language. The first foundation work was laid in C# 3. That was when features like lambda expressions and anonymous types were introduced, which later went on to form parts of LINQ in .NET 3.5.

After that, though, there wasn’t much new in terms of functional features for quite some time. In fact, it wasn’t really until the release of C# 7 in 2017 that FP seemed to become relevant again to the C# team. From C# 7 onward, every version of C# has contained something new and exciting to do more functional-style coding, a trend that doesn’t currently show any signs of stopping!

Chapter 2 introduced functional features that could be implemented in just about any C# codebase likely to still be in use out in the wild. In this chapter, we’re going to throw away that assumption and look at all the features you can use if your codebase is allowed to use any of the latest features—or at least those released since C# 7.

Tuples

Tuples were introduced in C# 7. NuGet packages do exist to allow some of the older versions of C# to use them too. They’re basically a way to throw together a quick-and-dirty collection of properties, without having to create and maintain a class.

If we have a few properties we want to hold onto for a minute in one place and then dispose of immediately, tuples are great for that.

If we have multiple objects we want to pass between ...

Get Functional Programming with C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.