A simple and easy to use option type for C#.
SimpleOption is a strongly typed alternative to null that helps to avoid null-reference exceptions, model your data more explictly and cut down on manual null checks.
SimpleOption is available via NuGet package manager:
PM> Install-Package Alterius.SimpleOption
Or via .NET CLI:
> dotnet add package Alterius.SimpleOption
Or visit https://www.nuget.org/packages/Alterius.SimpleOption
To use SimpleOption reference Alterius.SimpleOption.dll and import the following namespace:
usingAlterius.SimpleOption;Using static constructors:
varnone=Option.None<string>();varnoneWithException=Option.None<string>(newException());varsome=Option.Some("Something");Using implicit casting:
Option<string>option;option=(string)null;option=newException();option="Something";Using Option<T> as a method return type:
publicOption<string>GetString(objectobj){if(obj==null){returnnewArgumentNullException(nameof(obj));}varstr=_someRepo.GetString(obj);if(str==null){returnOption.None<string>();}returnstr;}Retrieving values is achieved by using the Match() method and its various overloads.
A basic example:
intx=option.Match(
some =>some+1,()=>-1);A good use of Option<T> is when retuning an IActionResult in a WebApi controller:
returnoption.Match<IActionResult>(
some =>Ok(some),()=>NotFound());Please note that in this example
TResultis declared explicitly asOk()andNotFound()do not return the same type, even though they both return an implementation ofIActionResult. This is not necessary under normal circumstances when the return types are identical.
Passing an instance of an exception to Option<T> allows you to handle application faults without the cost of throwing the exception:
returnoption.Match<IActionResult>(
some =>Ok(some),
e =>{if(eisNotFoundException)returnNotFound();returnBadRequest();});Warning! Accessing the value of the exception
(e)can result in aNullReferenceExceptionif there is no exception passed to the option and the result of the exceptionMatch()is none.
Using Option<T> as a method parameter:
publicboolHasString(Option<object>obj){returnobj.Match(
some =>string.IsNullOrEmpty(some.ToString()),()=>false);}A fluent interface is available as an alternative to the Match() method in version 1.0.0.1 and upwards:
intx=option.Some(some =>some+1).None(()=>-1);returnoption.Some<IActionResult>(some =>Ok(some)).None(()=>NotFound());returnoption.Some(some =>Ok(some)).None(e =>{if(eisNotFoundException)returnNotFound();returnBadRequest();});It's not recommended to mix the fluent interface with the
Match()method as it'll probably get confusing. Pick one style and stick with it.
