F#-like discriminated unions for C# with exhaustive compile-time matching, providing a type-safe alternative to polymorphism and exceptions.
OneOf is a C# library that implements F#-style discriminated unions, allowing a single type to represent one of several possible values. It solves the problem of returning multiple result types from methods without using exceptions or inheritance hierarchies, ensuring all cases are handled at compile time through exhaustive matching.
C# developers building APIs, libraries, or applications that require type-safe error handling, clear method signatures, and functional programming patterns like option types and pattern matching.
Developers choose OneOf for its compile-time safety, which eliminates runtime errors from unhandled cases, and its ability to make method contracts explicit, improving code readability and maintainability over traditional polymorphism or exception-based control flow.
Easy to use F#-like discriminated unions for C# with exhaustive compile time matching
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Forces handling of all possible cases through Match or Switch methods, eliminating runtime errors from unhandled scenarios as highlighted in the key features.
Makes API signatures clear by returning OneOf types like OneOf<User, InvalidName, NameTaken>, improving predictability and maintainability over vague return types.
Includes built-in types such as None, Some, Yes, and No in the OneOf.Types namespace, enabling easy implementation of option types and functional patterns in C#.
Automates code generation for OneOfBase hierarchies with a source generator, simplifying setup and maintenance as demonstrated in the README example.
Requires explicit Match or Switch calls for handling cases, which can be more verbose than C#'s built-in pattern matching or switch expressions, leading to cluttered code.
As a third-party library, it may not seamlessly integrate with all .NET tools or frameworks, and updates could introduce breaking changes without widespread community support.
Each OneOf instance wraps multiple types, adding slight memory and performance costs compared to direct type usage, especially in performance-sensitive scenarios.