CSharpEssentials.Any
3.0.1
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
<PackageReference Include="CSharpEssentials.Any" Version="3.0.1" />
<PackageVersion Include="CSharpEssentials.Any" Version="3.0.1" />
<PackageReference Include="CSharpEssentials.Any" />
paket add CSharpEssentials.Any --version 3.0.1
#r "nuget: CSharpEssentials.Any, 3.0.1"
#:package CSharpEssentials.Any@3.0.1
#addin nuget:?package=CSharpEssentials.Any&version=3.0.1
#tool nuget:?package=CSharpEssentials.Any&version=3.0.1
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:
SwitchandMatchmethods 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 | 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 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. |
-
.NETStandard 2.1
- CSharpEssentials.Core (>= 3.0.1)
- CSharpEssentials.Json (>= 3.0.1)
- System.Memory (>= 4.5.5)
- System.Text.Json (>= 8.0.5)
- System.Threading.Tasks.Extensions (>= 4.5.4)
-
net9.0
- CSharpEssentials.Core (>= 3.0.1)
- CSharpEssentials.Json (>= 3.0.1)
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 |