AlinSpace.Optional 1.0.3

Prefix Reserved
dotnet add package AlinSpace.Optional --version 1.0.3                
NuGet\Install-Package AlinSpace.Optional -Version 1.0.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AlinSpace.Optional" Version="1.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AlinSpace.Optional --version 1.0.3                
#r "nuget: AlinSpace.Optional, 1.0.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AlinSpace.Optional as a Cake Addin
#addin nuget:?package=AlinSpace.Optional&version=1.0.3

// Install AlinSpace.Optional as a Cake Tool
#tool nuget:?package=AlinSpace.Optional&version=1.0.3                

AlinSpace.Optional

NuGet version (AlinSpace.Optional)

AlinSpace Optional Types.

Why?

In C# reference types can either point to an object (be not "null") or not point to any object (be "null"). Value types can't be "null".

However, both reference and value types can be "default". But "default" does not mean the same for reference and value types. For reference types it means "null" and for value types it means the default value (e.g. for integer it is 0).

Optionals help with this problem, by making both reference and value types behave the same.

Examples

The following optional contains the value 5:

Optional<int> optional = Optional<int>.Some(5);

// Same as above.
Optional<int> optional = 5;

optional.HasValue // true
optional.HasNoValue // false
optional.Value // 5
optional.ValueOrDefault // 5

The following optional contains no value:

Optional<int> optional = Optional<int>.None();

optional.HasValue // false
optional.HasNoValue // true
optional.Value // throws OptionalHasNoValueException
optional.ValueOrDefault // 0

However be careful about "default". The optional will have a value:

Optional<int> optional = default;

optional.HasValue // true
optional.HasNoValue // false
optional.Value // 0
optional.ValueOrDefault // 0
Product Compatible and additional computed target framework versions.
.NET 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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.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.

Version Downloads Last updated
1.0.3 389 11/8/2022
1.0.1 352 11/7/2022
0.0.1 340 11/7/2022