CSharpEssentials.Any
6.5.2
dotnet add package CSharpEssentials.Any --version 6.5.2
NuGet\Install-Package CSharpEssentials.Any -Version 6.5.2
<PackageReference Include="CSharpEssentials.Any" Version="6.5.2" />
<PackageVersion Include="CSharpEssentials.Any" Version="6.5.2" />
<PackageReference Include="CSharpEssentials.Any" />
paket add CSharpEssentials.Any --version 6.5.2
#r "nuget: CSharpEssentials.Any, 6.5.2"
#:package CSharpEssentials.Any@6.5.2
#addin nuget:?package=CSharpEssentials.Any&version=6.5.2
#tool nuget:?package=CSharpEssentials.Any&version=6.5.2
CSharpEssentials.Any
Discriminated Unions (Sum Types) for .NET. A type-safe way to represent a value that can be one of several fixed types.
Features
- Unions of 2 to 8 types:
Any<T0, T1>throughAny<T0, ..., T7>. - Implicit Conversions: Assign values directly to the union type.
- Pattern Matching:
SwitchandMatchfor exhaustive handling. - Type Helpers:
Is<T>,As<T>,TryAs<T>,Deconstruct,ToTuple. - JSON Support: Built-in serialization.
Targets net11.0, net10.0, net9.0 and netstandard2.1. Depends on CSharpEssentials.Json and CSharpEssentials.Core. The types live in the CSharpEssentials.Any namespace.
Installation
dotnet add package CSharpEssentials.Any
Usage
Creating Unions
using CSharpEssentials.Any;
Any<string, int> ParseInput(string input)
{
if (int.TryParse(input, out int n))
return n; // implicit to Any<string, int>
return input;
}
Any<string, int> first = Any<string, int>.First("text"); // explicit factories: First, Second, Third ... Eighth
Any<string, int> second = Any.Create<string, int>(5); // or Any.Create with the type arguments spelled out
The union holds its active branch in Index (0 for the first type) and its value in Value (object?). Implicit conversion is ambiguous when two type arguments are the same type, for example Any<int, int>, and default(Any<...>) has Index 0 but a null Value, so IsFirst is true while GetFirst() returns null or default.
Pattern Matching
Any<string, int> result = ParseInput("123");
// Match returns AnyActionResult<TResult>: Status tells whether a handler ran, Result holds its value
AnyActionResult<string> output = result.Match(
first: str => $"String: {str}",
second: num => $"Number: {num}");
Console.WriteLine(output.Result);
AnyActionStatus status = result.Switch(
first: str => Console.WriteLine("It's a string"),
second: num => Console.WriteLine("It's a number"));
Every handler is optional. When the active branch has no handler, Switch returns AnyActionStatus.NotExecuted and Match returns an AnyActionResult whose Status is NotExecuted and whose Result is default; otherwise the status is Executed. Calling Match or Switch on default(Any<...>), or on a union that holds null, throws InvalidOperationException.
Type Checking
if (result.IsSecond)
{
int val = result.GetSecond(); // throws InvalidOperationException when another branch is active
}
// Generic helpers: the target type first, then the union's type arguments
bool isInt = result.Is<int, string, int>();
int? asInt = result.As<int, string, int>();
bool found = result.TryAs<int, string, int>(out int value);
// Deconstruct and ToTuple: the inactive branches are default
var (text, number) = result;
(string? First, int Second) tuple = result.ToTuple();
IsFirst, IsSecond, IsThird through IsEighth and GetFirst() through GetEighth() exist up to the arity of the union. Is, As and TryAs test the runtime type of the value, not the branch. For a value-type target, As returns default (so 0 for int) when the value is of another type, not null; use Is or TryAs to tell the cases apart.
A union serializes to JSON as {"Index":1,"Value":123} and deserializes back to the same branch. ToString() returns the JSON of the value.
Multiple Types
Any<string, int, Exception> response = GetData(5);
response.Switch(
first: s => Console.WriteLine($"Success: {s}"),
second: i => Console.WriteLine($"NotFound: {i}"),
third: e => Console.WriteLine($"Error: {e.Message}"));
Collection Extensions
Scatter a sequence of unions into typed arrays in one pass:
// Classify API responses into successes and errors
var (users, errors) = responses
.Traverse(r => ClassifyResponse(r)); // returns Any<UserDto, ApiError>
// Or partition an existing sequence of unions
var (drafts, published) = articles
.Select(a => GetState(a)) // returns Any<Draft, Published>
.Partition();
Partition and Traverse are available for all arities (Any<T0,T1> through Any<T0,...,T7>), returning a tuple with one typed array per variant.
| Product | Versions 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 is compatible. 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. net11.0 is compatible. |
| .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. |
-
.NETStandard 2.1
- CSharpEssentials.Core (>= 6.5.2)
- CSharpEssentials.Json (>= 6.5.2)
- System.Text.Json (>= 9.0.4)
-
net10.0
- CSharpEssentials.Core (>= 6.5.2)
- CSharpEssentials.Json (>= 6.5.2)
-
net11.0
- CSharpEssentials.Core (>= 6.5.2)
- CSharpEssentials.Json (>= 6.5.2)
-
net9.0
- CSharpEssentials.Core (>= 6.5.2)
- CSharpEssentials.Json (>= 6.5.2)
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 |
|---|---|---|
| 6.5.2 | 111 | 10/10/2026 |
| 6.5.1 | 108 | 10/10/2026 |
| 6.5.0 | 107 | 10/9/2026 |
| 6.4.0 | 111 | 10/9/2026 |
| 6.3.0 | 118 | 10/8/2026 |
| 6.2.0 | 110 | 10/8/2026 |
| 6.1.0 | 111 | 10/7/2026 |
| 6.0.0 | 112 | 10/7/2026 |
| 5.2.1 | 111 | 10/7/2026 |
| 5.2.0 | 112 | 10/7/2026 |
| 5.1.0 | 110 | 10/7/2026 |
| 5.0.0 | 109 | 10/6/2026 |
| 4.1.0 | 116 | 10/6/2026 |
| 4.0.0 | 113 | 10/5/2026 |
| 3.2.3 | 286 | 5/31/2026 |
| 3.2.2 | 161 | 5/31/2026 |
| 3.2.1 | 156 | 5/31/2026 |
| 3.2.0 | 156 | 5/30/2026 |
| 3.1.0 | 170 | 5/27/2026 |
| 3.0.8 | 154 | 5/20/2026 |