Functional Programming in C#
Start programming functionally in C# by reading Functional Programming in C# and using
I started my professional programming career mostly writing C#. I had learned to program about a decade before and had mostly used Java after that, so the transition to C# was easy for me. In C#, my style was initially object-oriented and continued to be so for the first couple years.
Around that time, the idea to write more functionally was in the air at our company. I debated whether I should first switch to a functional language like F# and then learn functional programming or stick with C# and learn functional programming there. Surely learning F# (in my free time) would be easier than learning functional programming, right? Then I could learn functional programming in a language that was designed for it.
I decided that the best approach was to stick with C#. The main reason is that I was using C# every day at work, so while I am trying to learn functional programming in C# with my limited free time, I can practice using the new concepts that I am learning while writing C# at work.
If you are a C# programmer with an object-oriented style and want to begin your journey into functional programming, then here are my top recommendations.
Enrico Buonanno is the author of the book Functional Programming in C#. This is the prefect book on functional programming for C# programmers. It is beyond satisfying to see functional programming examples in a language that is so familiar to me. Even better is that the examples are pragmatic and focus on line-of-business application scenarios.
The book is published by Manning Publications, and the link above is to their page for this book. At that page, you can buy this book in paper or electronic form. I recommend buying both. Immediately after being told about this book, I bought paper and electronic copies. A nice benefit of buying this book from Manning is access to an exclusive place to comment on the book. Enrico Buonanno replied to many of the questions that I asked there.
language-ext is a base class library for functional programming in C#. It is the best library for this purpose. It is led by Paul Louth. He has the heart of a teacher. It is not uncommon for Paul to respond at length to user questions. See the issues tagged
examples / documentation. He has also written extensive documentation in the wiki.
Supporting Paul and this library are a good number of contributors. I started my journeys into both functional programming and contributing to open source through
language-ext. Especially because of my initial activity, I currently have the second most commits. I am less active these days, but I am still watching and will answer questions or fix bugs if I have the necessary knowledge. If you have a question about
language-ext, then create an issue and Paul, I, or someone else will answer your question.
Mark Seemann is a Microsoft MVP, conference speaker, and teacher with a video series at Clean Coders and courses at Pluralsight. On top of that, he is a prolific blogger. Many of his blog posts are aimed at C# programmers. The example code on his blog is written in C#, F#, or Haskell, and often the same example is given in more than one language. One way I routinely encounter ideas in functional programming that are new to me is by reading his latest blog posts. I often comment on his posts, and he always responds.
To consider an example, my favorite blog post is Composite as a monoid. It became my favorite after taking his workshop From Design Patterns to Universal Abstractions at NDC Minneapolis 2019. The main thing I learned from the workshop was that a monoid, which is essentially the same as the composite pattern, is the correct way to expose impure behavior.
Functional programming is famous (and possibly infamous) for the concept of a monad. Much ink has been spilled and ASCII characters displayed in attempts to explain this concept. This is partly because it is a difficult concept to understand. It look me a long time to reach my current understanding, and I know there is more on this topic that I want to learn.
I think the best source for a C# programmer to begin forming a precise understanding of monads is by reading Eric Lippert's blog post series on monads. It begins at that link and includes thirteen posts. Eric Lippert was a member of the C# language design time, and has many highly voted answers for questions tagged C# on StackOverflow. That is to say, he knowns a lot about about C#, and he spends lots of time explaining the complicated parts of C# to others.
I say "begin forming a precise understanding" because an explanation of monads is not required to start using them, which is much more important than "understanding them". In fact, you are probably already using monads in C# without realizing it. Types like
Task<T> are monads because of intensional choices by language designers like Eric Lippert. It is not necessary to study monads in order to program functionally in C#. Instead, at some point on your journey into functional programming, you will want gain a better understanding of monads. That is when you should begin reading those posts.
There are great resources for C# programmers to learn about functional programming. Start by buying both the paper and electronic copies of Enrico Buonanno's Functional Programming in C#. As you read that, begin incorporating what you learn into your C# programs at work with help from
language-ext. Keep up your motivation to learn functional programming by subscribing to blog posts by Mark Seemann, and read Eric Lippert's blog post series on monads when you are ready to challenge yourself and gain a better understanding of monads.
The tags feature of Coding Blog Plugin is still being developed. Eventually the tags will link somewhere.