This library is designed to avoid throwing exceptions in the code. Instead, it returns a result from the method and describes the error. The library helps reduce the number of try-catch blocks in the code and is less taxing on the program compared to catching and rethrowing exceptions.
$ dotnet add package ReturnPatternC#Library designed for graceful operation result handling without using exceptions for control flow. It provides a clear and predictable way to return a value on success or error information on failure, leading to cleaner and more robust code. Who is this library for? This library is created for the entire C# and .NET world, offering a modern approach to handling results and errors in applications of any scale. If you strive for a more functional programming style and want to reduce reliance on try-catch blocks for handling expected failures, ReturnPattern is for you.
You can easily add ReturnPattern to your project using the NuGet Package Manager:
FeaturesExplicit Result Representation:
Clearly indicates whether an operation succeeded IsSuccess or failed IsFailure, and provides access to Value or Error.Controlled Error Handling:
Allows returning specialized exceptions or simple string messages on errors, giving you full control.Clean and Concise API:
Simplifies result handling logic, making code more readable and maintainable.
ReturnPattern offers three main Return types for different scenarios: with a value type and an error type, with only a value type with Exception as the default error, and without types for operations that simply indicate success/failure.
using ReturnPattern;
using System;
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("--- Example Return<TValue, TError> ---");
PatternResultTValueAndTError();
Console.WriteLine("\n--- Example Return<TValue> ---");
PatternResultTValue();
Console.WriteLine("\n--- Example Return ---");
PatternResult();
}
static void PatternResultTValueAndTError()
{
Return<double, DivideByZeroException> r = Divide(4.5, 7.8);
if (r.IsSuccess)
{
Console.WriteLine($"Success: {r.IsSuccess}");
Console.WriteLine($"Result: {r.Value}");
}
else
{
Console.WriteLine($"Error: {r.Message}");
}
}
static void PatternResultTValue()
{
Return<int> r = IsSix(6);
if (r.IsSuccess)
{
Console.WriteLine($"Success: {r.IsSuccess}");
Console.WriteLine($"Result: {r.Value}");
}
else
{
Console.WriteLine($"Error: {r.Message}");
}
}
static void PatternResult()
{
Return r = StringIsEmpty(string.Empty);
if (r.IsSuccess)
{
Console.WriteLine($"Success: {r.IsSuccess}");
}
else
{
Console.WriteLine($"Error: {r.Message}");
}
}
// Helper methods demonstrating Return usage
static Return<double, DivideByZeroException> Divide(double a, double b)
{
if (b == 0)
{
return Return.Failure(new DivideByZeroException(), 0.0);
}
return Return.Success<double, DivideByZeroException>(a / b);
}
static Return<int> IsSix(int value)
{
if(value is 6)
{
return Return.Success(value);
}
return Return.Failure($"{value} is not 6", 0);
}
static Return StringIsEmpty(string value)
{
if(value == string.Empty)
{
return Return.Success();
}
return Return.Failure($"{value} is not empty");
}
}
We've updated your library for improved reliability! The HasValue property is now deprecated; please use IsSuccess instead. IsSuccess logic is updated: it's now true only when Value is not null, ensuring its availability. Also, the Result class has been renamed to Return for better clarity in representing method outcomes. The implicit Return to bool conversion is deprecated too; check status using IsSuccess or IsFailure to always access error details. These changes make your code more predictable and safer.
🛠 Author: Fishman 📅 Last updated: 11.2025
This project is licensed under the Boost Software License – Version 1.0 – August 17th, 2003.