⚠ Deprecated: Legacy
Suggested alternative: ReturnPattern
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 ResultPatternThis 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.
HasValue Property: Added to the Result<TValue, TError> class. This property indicates whether the result has a value (is not null).Success Method: Added to the Result class. This method creates a successful result with a null value, providing a convenient way to signify success without an associated value.Version 1.2.0 of the program includes several improvements and new features. This update focuses on enhancing functionality by adding new methods and fixing existing issues. Below are the detailed changes.
Failure<TValue>(string message, TValue value)
message: The error message.value: The value associated with the failed result.Failure<TValue, TError>(TError error, TValue value)
error: The error associated with the failed result.value: The value associated with the failed result.Failure<TValue>(Exception error, TValue value)
error: The exception associated with the failed result.value: The value associated with the failed result.Success<TValue>(TValue value)
value: The value associated with the successful result.Success<TValue, TError>(TValue value)
value: The value associated with the successful result.
using ResultPattern;
PatternResultTValueAndTError();
PatternResultTValue();
PatternResult();
static void PatternResultTValueAndTError()
{
Result<double, DivideByZeroException> r = Divide(4.5, 7.8);
if (r)
{
Console.WriteLine($"Success: {r.IsSuccess}");
Console.WriteLine($"Result: {r.Value}");
}
else
{
Console.WriteLine($"Error: {r.Message}");
}
}
static void PatternResultTValue()
{
Result<int> r = IsSix(6);
if (r)
{
Console.WriteLine($"Success: {r.IsSuccess}");
Console.WriteLine($"Result: {r.Value}");
}
else
{
Console.WriteLine($"Error: {r.Message}");
}
}
static void PatternResult()
{
var r = StringIsEmpty(string.Empty);
if (r)
{
Console.WriteLine($"Success: {r.IsSuccess}");
}
else
{
Console.WriteLine($"Error: {r.Message}");
}
}
static Result<double, DivideByZeroException> Divide(double a, double b)
{
if (b == 0)
{
return Result<double, DivideByZeroException>.Failure(new DivideByZeroException());
}
return Result<double, DivideByZeroException>.Success(a / b);
}
static Result<int> IsSix(int value)
{
if(value is 6)
{
return Result<int>.Success(value);
}
return Result<int>.Failure($"{value} is not 6");
}
static Result StringIsEmpty(string value)
{
if(value == string.Empty)
{
return Result.Success();
}
return Result.Failure($"{value} is not empty");
}