CSharpEssentials.Any 3.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package CSharpEssentials.Any --version 3.0.1
                    
NuGet\Install-Package CSharpEssentials.Any -Version 3.0.1
                    
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="CSharpEssentials.Any" Version="3.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSharpEssentials.Any" Version="3.0.1" />
                    
Directory.Packages.props
<PackageReference Include="CSharpEssentials.Any" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CSharpEssentials.Any --version 3.0.1
                    
#r "nuget: CSharpEssentials.Any, 3.0.1"
                    
#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.
#:package CSharpEssentials.Any@3.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CSharpEssentials.Any&version=3.0.1
                    
Install as a Cake Addin
#tool nuget:?package=CSharpEssentials.Any&version=3.0.1
                    
Install as a Cake Tool

CSharpEssentials.Any

CSharpEssentials.Any provides a robust implementation of Discriminated Unions (also known as Sum Types or Coproducts) for .NET. It allows you to define a type that can hold a value of one of several fixed types, providing type safety and exhaustive pattern matching.

🚀 Features

  • Discriminated Unions: Support for unions of 2 up to 8 types (Any<T0, T1>, Any<T0, T1, T2>, etc.).
  • Type Safety: Compiler-checked access to values.
  • Pattern Matching: Switch and Match methods to handle each case.
  • Implicit Conversions: Seamlessly convert values to their union type.
  • JSON Support: Built-in serialization handling.

📦 Installation

dotnet add package CSharpEssentials.Any

🛠 Usage

1. Defining a Union

You can use Any<T0, T1> as a return type or property.

using CSharpEssentials.Any;

public Any<string, int> ParseOrReturnOriginal(string input)
{
    if (int.TryParse(input, out int result))
    {
        return result; // Implicit conversion to Any<string, int>
    }
    return input; // Implicit conversion
}

2. Pattern Matching (Match)

Use Match to transform the value based on which type it holds.

Any<string, int> result = ParseOrReturnOriginal("123");

string output = result.Match(
    first: str => $"It's a string: {str}",
    second: num => $"It's a number: {num}"
);

3. Executing Actions (Switch)

Use Switch to execute code based on the type.

result.Switch(
    first: str => Console.WriteLine("String"),
    second: num => Console.WriteLine("Number")
);

4. Checking and Accessing

if (result.IsSecond)
{
    int val = result.GetSecond();
}

5. Handling more types

You can handle up to 8 variations.

// A result that can be Success (string), NotFound (int code), or Error (Exception)
public Any<string, int, Exception> GetData(int id)
{
    // ... logic
    return new Exception("Failed"); 
}

var data = GetData(5);

data.Switch(
    first: s => Console.WriteLine($"Success: {s}"),
    second: i => Console.WriteLine($"Not Found: {i}"),
    third: e => Console.WriteLine($"Error: {e.Message}")
);
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on CSharpEssentials.Any:

Package Downloads
CSharpEssentials

A comprehensive C# library enhancing functional programming capabilities with type-safe monads (Maybe, Result), discriminated unions (Any), and robust error handling. Features include: domain-driven design support, enhanced Entity Framework integration, testable time management, JSON utilities, and LINQ extensions. Built for modern C# development with focus on maintainability, testability, and functional programming principles.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.5 122 5/7/2026
3.0.4 126 5/6/2026
3.0.3 112 5/5/2026
3.0.2 130 5/5/2026
3.0.1 131 5/3/2026
3.0.0 132 5/3/2026
2.1.0 297 11/26/2025
2.0.9 240 9/30/2025
2.0.8 235 9/29/2025
2.0.7 222 9/29/2025
2.0.6 219 9/29/2025
2.0.5 231 9/29/2025
2.0.4 214 9/28/2025
2.0.3 224 9/28/2025
2.0.2 234 9/28/2025
2.0.1 233 9/28/2025
2.0.0 228 9/28/2025