Eliminating Nulls in C# with Functional Programming
This is a short and sweet article showing how the Option
class can bring functional programming concepts to C# codebases and prevent null reference exceptions.
This article will be using the extremely popular language-ext library as the most popular .NET functional programming library built for object oriented languages (aside from F#).
Installing the Language-Ext Library
In order to work with the language-ext library, you will need to add a NuGet reference to the package. This can be done via NuGet Package Manager in Visual Studio or by running Install-Package LanguageExt.Core
from the package manager console.
What is an Option?
Options are a reference type that either contain some value or have none. As an example, look at the following method that searches a dictionary and returns a value from it:
In this method, since the return type is Option<ResumeKeyword>
, the return type will either be of Some<ResumeKeyword>
or None
. The…