Optional2 4.4.0
See the version list below for details.
Requires NuGet 2.12 or higher.
dotnet add package Optional2 --version 4.4.0
NuGet\Install-Package Optional2 -Version 4.4.0
<PackageReference Include="Optional2" Version="4.4.0" />
paket add Optional2 --version 4.4.0
#r "nuget: Optional2, 4.4.0"
// Install Optional2 as a Cake Addin #addin nuget:?package=Optional2&version=4.4.0 // Install Optional2 as a Cake Tool #tool nuget:?package=Optional2&version=4.4.0
Optional2
Optional is a robust option/maybe type for C#. Originally developed by Nils Lück - Optional
What and Why?
Optional is a strongly typed alternative to null values that lets you:
- Avoid those pesky null-reference exceptions
- Signal intent and model your data more explictly
- Cut down on manual null checks and focus on your domain
- Work with optional values of reference and value types in the same way. Two versions of the same generic method with
where T : class
andwhere T : struct
are no longer needed
Installation
PM> Install-Package Optional2
NuGet package. Supports .NET 3.5+ and .NET (.NET Standard 1.0+)
Usage
Using the library
To use Optional simply import the following namespace:
using Optional;
A few auxiliary namespaces are provided:
using Optional.Linq; // Linq query syntax support
using Optional.Unsafe; // Unsafe value retrieval
using Optional.Collections; // Linq like methods with Option specifics
Creating optional values
// The most basic way to create optional values is to use the static `Option` class:
var none = Option.None<int>();
var some = Option.Some(10);
// or use extension methods:
var none = 10.None(); // Equivalent to Option.None<int>()
var some = 10.Some();
Option can be filtered during creation by methods .Some*()
or .None*()
. The most useful from them is .SomeNotNull()
since Nullable Reference Types are supported. Analogue for value types is .ToOption()
.
Retrieving values
When retrieving values, Optional forces you to consider both cases (that is if a value is present or not).
Like Nullable<T>
Option can be tested by HasValue
property.
There are also more precise ways:
var isThousand = option.Contains(1000);
var isGreaterThanThousand = option.Exists(val => val > 1000);
Ways to retrieve a value from Option are:
var value = option.ValueOr(10); // Returns the value if Some, or otherwise an alternative value (10)
var value = option.Match(
some: x => x + 1,
none: () => 10
); // pattern matching
var value = option.ValueOrFailure(); // Unsafe: throws OptionValueMissingException on None
Transforming and filtering values
var value = 10.Some();
var doubled = value.Map(x => x * 2); // Some(20)
var odd = doubled.Filter(x => x % 2 == 1); // None
var fallback = odd.Else(1.Some()); // Some(1)
For details see Option<T>
or explore xml doc.
Other utilities
Monadic evaluation with LINQ query syntax
using Optional.Linq;
var personWithGreenHair =
from person in FindPersonById(10)
from hairstyle in GetHairstyle(person)
where hairstyle.Color == "green"
select person;
Options with exceptional values aka Either
An Option<T, TException>
type with similar capabilities.
var none = Option.None<int, ErrorCode>(ErrorCode.GeneralError);
var some = Option.Some<int, ErrorCode>(10);
Working with collections
IEnumerable<T>
related Linq similar methods(First|Last|Signle)OrNone()
items.Values()
keeps only Some fromitems
and unwraps themdictionary.GetValueOrNone(key: 42)
- lookups an entry by key
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp1.0 was computed. netcoreapp1.1 was computed. netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard1.0 is compatible. netstandard1.1 was computed. netstandard1.2 was computed. netstandard1.3 was computed. netstandard1.4 was computed. netstandard1.5 was computed. netstandard1.6 was computed. netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net35 is compatible. net40 is compatible. net403 was computed. net45 is compatible. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen30 was computed. tizen40 was computed. tizen60 was computed. |
Universal Windows Platform | uap was computed. uap10.0 was computed. |
Windows Phone | wp8 was computed. wp81 was computed. wpa81 was computed. |
Windows Store | netcore was computed. netcore45 was computed. netcore451 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 3.5
- No dependencies.
-
.NETFramework 4.0
- No dependencies.
-
.NETFramework 4.5
- No dependencies.
-
.NETStandard 1.0
- NETStandard.Library (>= 1.6.1)
-
.NETStandard 2.0
- No dependencies.
-
.NETStandard 2.1
- No dependencies.
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.