ErrorOr for C# (.NET): Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

ErrorOr for C# (.NET): Complete Guide to Error Handling Without Exceptions

\n\n

In modern C# (.NET) development, error handling is traditionally built on the exception mechanism. However, this approach has several drawbacks: exceptions disrupt the execution flow, create stack overhead, and, most importantly, do not always explicitly signal to the developer that a method may fail. The ErrorOr library offers an alternative — a functional approach where the result of an operation is returned as a special object that can contain either a successful value or a list of errors.

\n\n

The main idea of ErrorOr is to make error handling explicit and type-safe. Instead of throwing an exception, the method returns an object of type ErrorOr<TValue>. The calling code must check whether the operation completed successfully before using the result. This prevents many bugs related to unhandled exceptions and makes the code more predictable and easier to understand.

\n\n

The library is inspired by languages with advanced type systems, such as Rust (the Result<T, E> type) and F# (the Result<'T, 'TError> type). ErrorOr is ideal for Clean Architecture, Domain-Driven Design (DDD), and CQRS projects, where it is important to clearly separate successful and error scenarios. It also works great with the "Railway Oriented Programming" pattern, allowing you to build chains of operations with automatic error propagation.

\n\n

Installation

\n\n

Installing the ErrorOr library into a .NET project is done via the NuGet package manager. Execute one of the following commands in the terminal or Package Manager Console:

\n\n
dotnet add package ErrorOr
\n\n

Or, if you are using the Package Manager Console in Visual Studio:

\n\n
Install-Package ErrorOr
\n\n

After installation, include the namespace in your code file:

\n\n
using ErrorOr;
\n\n

Quick Start: Minimal Working Example

\n\n

Let's write a simple function that divides one number by another. Instead of throwing an exception when dividing by zero, we will return ErrorOr<double>.

\n\n
using ErrorOr;\n\npublic static class Calculator\n{\n    public static ErrorOr<double> Divide(double dividend, double divisor)\n    {\n        if (divisor == 0)\n        {\n            // Return an error if the divisor is zero\n            return Error.Validation("Calculator.DivisionByZero", "Divisor cannot be zero.");\n        }\n\n        // Return a successful result\n        return dividend / divisor;\n    }\n}\n\n// Usage\npublic class Program\n{\n    public static void Main()\n    {\n        ErrorOr<double> result = Calculator.Divide(10, 2);\n\n        if (result.IsError)\n        {\n            Console.WriteLine($"Operation failed: {result.FirstError.Description}");\n        }\n        else\n        {\n            Console.WriteLine($"Result: {result.Value}");\n        }\n\n        // Example with an error\n        ErrorOr<double> errorResult = Calculator.Divide(10, 0);\n        if (errorResult.IsError)\n        {\n            Console.WriteLine($"Operation failed: {errorResult.FirstError.Description}");\n        }\n    }\n}
\n\n

In this example, we see the key elements: creating an error via Error.Validation(), returning a successful value (implicit conversion), and checking the result via the IsError property.

\n\n

Main Methods, Functions, and Classes

\n\n

Below is a detailed breakdown of the key components of the ErrorOr library. For each element, the signature, description, and a working code example are provided.

\n\n

1. Class ErrorOr

Recommendations