Preface

I attend developers’ conferences as often as I’m able and have noticed a trend. Each year, there always seems to be more content about functional programming (FP), not less. Often an entire track is dedicated to it, and the other talks include FP content somewhere as a talking point.

FP is slowly but surely becoming a big deal. Why is that?

Because FP is one of the greatest innovations in the history of software development. It’s also cool. Fun as well.

With the growth of concepts like containerization and serverless applications, FP isn’t just a bit of fun for developers’ free-time projects; it’s not a fad that’ll be forgotten in a few years. It has real benefit to bring to our stakeholders.

Further, in the .NET world, several additional factors are at play. Mads Torgerson, the C# lead designer, is himself a fan of FP, and one of the major driving forces behind the adoption of the functional paradigm into .NET. There’s also F#, the .NET functional language. Because F# and C# share a common runtime, many functional features requested by the F# team often become available in C# in some form as well.

The big questions remain, though: What is FP? And will I need to learn an entire new programming language just to be able to use it? The good news is that if you’re a .NET developer, you don’t need to spend large chunks of your own time learning a new technology just to stay up-to-date. You don’t even have to invest in another third-party library to add to your application’s dependencies: FP is possible with out-of-the-box C# code, albeit with a little tinkering around.

This book introduces all the fundamental concepts of FP, demonstrates their benefits, and describes how they might be achieved in C#—not just for your own hobby programming, but with a real eye toward bringing immediate benefit to your work life as well.

These benefits include things such as the following:

  • Code that is cleaner, tidier, and easier to read

  • An easier-to-maintain codebase

  • Applications that are significantly less likely to suffer from unhandled exceptions and their unpredictable consequences

  • A codebase that’s easier to write automated unit tests for

All this and more!

Who Should Read This Book?

This book is intended for developers—whether professional, student, or hobbyist—who already have a basic grounding in C#. You don’t need to be an expert but do need to be familiar with the basics and feel comfortable putting together at least a relatively simply C# application.

Some more advanced .NET topics are covered, but I’ll provide explanations when they come up.

This book has been written with a few categories of people in mind:

  • Those of you who have learned the basics of C# but want to find ways to take your learning further via more advanced techniques for writing better, more robust code.

  • .NET developers who have heard of FP and perhaps even know what it is, but want to know how to get started writing code that way in C#.

  • F# developers looking for ways to keep using the functional toys you’re used to.

  • Those migrating to .NET from another functional, or functional-supporting, language (like Java).

  • Anyone who really, truly loves coding. If you spend all day writing code in the office, then come home to write more for fun, this book is probably for you.

Why I Wrote This Book

I’ve been interested in programming for as long as I can remember. When I was a young boy, we had a ZX Spectrum, a British home computer developed by Sinclair Research in the early ’80s. If anyone remembers the Commodore 64, it was a bit like that but far more primitive. It had just 15 colors and a screen resolution of 256 × 192.1 I had the more advanced model with 48K of memory, though my Dad had the earlier machine, the ZX81, which had a single kilobyte of memory available (and rubber keys). It couldn’t even have colored-in character sprites, just areas of the screen, so your game avatar would change color to that of whatever they were standing in front of. In short, it was pure awesome on toast.

One of the best things about it was its OS that effectively consisted of a text-based programming interface, and code was required to load a game (from a cassette tape, with the command LOAD ""), but magazines and books for kids were available with code you could enter yourself for games. It was from these that I developed my lasting obsession with the mysteries of computer code. Thanks so much, Usborne Publishing!

When I was around 14 years old or so, a computer-based careers advice program at school suggested I could think about taking up a career in software development. This is the first time I realized that you could take this silly hobby and turn it into something that you could actually make money from!

After university was over, it was time to get a proper job, and that was where I got my first exposure to C#. So the next step, I supposed, was to learn how to develop code properly. Easy, right? I’ll be honest, nearly two decades on, I’m still trying to work that out.

One of the big turning points for me in my programming career was attending a developers’ conference in Norway and finally starting to understand what this functional programming thing I’d been hearing about was actually about. Functional code is elegant, concise, and easy to read in a way that other forms of code just don’t seem to be. As with any type of code, writing horrible-looking codebases is still possible, but the coding still fundamentally feels like it’s finally being done properly, in a way that I’ve never really felt from other styles of coding. Hopefully, after reading this book, you’ll not only agree but also be interested in searching out the many other avenues that exist out there for exploring it further.

Navigating This Book

This is how I’ve organized this book:

  • Chapter 1 is the introduction and covers what you can do right now to start coding functionally in C#, without having to reference a single new NuGet package, use a third-party library, or hack around with the language. Nearly all the examples in this chapter work with just about every version of C# since version 3. This chapter presents the very first steps into FP, all fairly easy code, which sets the groundwork for what’s to come.

  • Part I, “What Are We Already Doing?” (Chapters 2 through 4), is about ways to adopt a few functional ideas into your daily C# coding without needing to do anything that’s fundamentally all that unfamiliar. Many of the code samples are straightforward, out-of-the-box C#. Try these chapters if you’ve never heard of FP before and want a nice, gentle introduction. Think of it as dipping your toes in the water to see if you’re interested in going for a swim.

  • Part II, “Into the Belly of the Functional” (Chapters 5 through 10), is where the gloves come off, and I start introducing some “proper” functional concepts. Don’t panic, though; I’m going to be taking the gentle slope up to the summit, and will introduce ideas slowly and in easy-to-digest, bite-size pieces.

  • Part III, “And Out the Other Side” (Chapters 11 through 14), wraps up and consolidates everything you’ve learned so far, as well as suggests where you might consider ways to continue learning further.

Feel free to dive in at the level you feel ready for. This isn’t a novel;2 read the chapters in the order that makes sense to you.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Note

This element signifies a general note.

Warning

This element indicates a warning or caution.

Using Code Examples

Supplemental material (code examples, exercises, etc.) is available for download at https://oreil.ly/functional-programming-with-csharp-code.

If you have a technical question or a problem using the code examples, please send email to .

This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but generally do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Functional Programming with C# by Simon J. Painter (O’Reilly). Copyright 2023 Simon J. Painter, 978-1-492-09707-5.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

O’Reilly Online Learning

Note

For more than 40 years, O’Reilly Media has provided technology and business training, knowledge, and insight to help companies succeed.

Our unique network of experts and innovators share their knowledge and expertise through books, articles, and our online learning platform. O’Reilly’s online learning platform gives you on-demand access to live training courses, in-depth learning paths, interactive coding environments, and a vast collection of text and video from O’Reilly and 200+ other publishers. For more information, visit https://oreilly.com.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at https://oreil.ly/functional-programming-with-csharp.

For news and information about our books and courses, visit https://oreilly.com.

Find us on LinkedIn: https://linkedin.com/company/oreilly-media

Follow us on Twitter: https://twitter.com/oreillymedia

Watch us on YouTube: https://youtube.com/oreillymedia

Acknowledgments

The very first person to thank is Kathleen Dollard. She gave a talk at the Norwegian Developers Conference (NDC) at Oslo some years ago called “Functional Techniques for C#.” It was the first real exposure I’d ever had to FP, and it was a real eye-opener.

The other guru I’ve followed on this trail is Enrico Buonanno, whose book Functional Programming in C# (Manning) was the first that allowed me to properly understand how some of the hard-to-grasp functional concepts worked. If you enjoy my book, please check out his next.

Ian Russell, Matthew Fletcher, Liam Riley, Max Dietze, Steve “Talks Code” Collins, Gerardo Lijs, Matt Eland, Rahul Nath, Siva Gudivada, Christian Horsdal, Martin Fuß, Dave McCollough, Sebastian Robbins, David Schaefer, Peter De Tender, Mark Seeman, Gerald Versluis, Alex Wild, Valadis Novakovits, Lackner Reinhard, Eric Lucas, Christopher Straten, Kathleen Dollard, and Scott Wlaschin read the early drafts and provided invaluable feedback. Thanks, folks!

Thanks also to my editor, Jill Leonard. She must have the patience of a saint to put up with me for a whole year!

1 It had eight base colors and a bright version of each. One was black, though, and how on earth can you have bright black? So…​15.

2 But if this were a novel, you can guarantee it’d be a murder mystery, and the butler would have done it!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
54.175.70.29