From e1974948ef44af0ca000eb61ba014c9828c093ea Mon Sep 17 00:00:00 2001
From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com>
Date: Mon, 30 Mar 2026 17:25:21 -0500
Subject: [PATCH] fix: add checks for conditional compilation
---
src/StevanFreeborn.Results/Result.cs | 132 ++++++----
.../ResultAsyncExtensions.cs | 240 ++++++++++++------
2 files changed, 248 insertions(+), 124 deletions(-)
diff --git a/src/StevanFreeborn.Results/Result.cs b/src/StevanFreeborn.Results/Result.cs
index d64262f..b1ee909 100644
--- a/src/StevanFreeborn.Results/Result.cs
+++ b/src/StevanFreeborn.Results/Result.cs
@@ -53,15 +53,23 @@ namespace StevanFreeborn.Results
/// Thrown when func or errorHandler is null.
public static Result Try(Func func, Func errorHandler) where TError : IError
{
- if (func is null)
- {
- throw new ArgumentNullException(nameof(func));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(func);
+ #else
+ if (func is null)
+ {
+ throw new ArgumentNullException(nameof(func));
+ }
+ #endif
- if (errorHandler is null)
- {
- throw new ArgumentNullException(nameof(errorHandler));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(errorHandler);
+ #else
+ if (errorHandler is null)
+ {
+ throw new ArgumentNullException(nameof(errorHandler));
+ }
+ #endif
try
{
@@ -142,10 +150,14 @@ namespace StevanFreeborn.Results
/// Thrown when mapper is null.
public Result Map(Func mapper)
{
- if (mapper is null)
- {
- throw new ArgumentNullException(nameof(mapper));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(mapper);
+ #else
+ if (mapper is null)
+ {
+ throw new ArgumentNullException(nameof(mapper));
+ }
+ #endif
return IsSuccess ? Result.Ok(mapper(Value)) : Result.Fail(Error);
}
@@ -158,10 +170,14 @@ namespace StevanFreeborn.Results
/// Thrown when mapper is null.
public Result MapError(Func mapper) where TNewError : IError
{
- if (mapper is null)
- {
- throw new ArgumentNullException(nameof(mapper));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(mapper);
+ #else
+ if (mapper is null)
+ {
+ throw new ArgumentNullException(nameof(mapper));
+ }
+ #endif
return IsFailure ? Result.Fail(mapper(Error)) : Result.Ok(Value);
}
@@ -175,10 +191,14 @@ namespace StevanFreeborn.Results
/// Thrown when binder is null.
public Result Bind(Func> binder)
{
- if (binder is null)
- {
- throw new ArgumentNullException(nameof(binder));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(binder);
+ #else
+ if (binder is null)
+ {
+ throw new ArgumentNullException(nameof(binder));
+ }
+ #endif
return IsSuccess ? binder(Value) : Result.Fail(Error);
}
@@ -193,15 +213,23 @@ namespace StevanFreeborn.Results
/// Thrown when onSuccess or onFailure is null.
public TResult Match(Func onSuccess, Func onFailure)
{
- if (onSuccess is null)
- {
- throw new ArgumentNullException(nameof(onSuccess));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(onSuccess);
+ #else
+ if (onSuccess is null)
+ {
+ throw new ArgumentNullException(nameof(onSuccess));
+ }
+ #endif
- if (onFailure is null)
- {
- throw new ArgumentNullException(nameof(onFailure));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(onFailure);
+ #else
+ if (onFailure is null)
+ {
+ throw new ArgumentNullException(nameof(onFailure));
+ }
+ #endif
return IsSuccess ? onSuccess(Value) : onFailure(Error);
}
@@ -214,15 +242,23 @@ namespace StevanFreeborn.Results
/// Thrown when onSuccess or onFailure is null.
public void Match(Action onSuccess, Action onFailure)
{
- if (onSuccess is null)
- {
- throw new ArgumentNullException(nameof(onSuccess));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(onSuccess);
+ #else
+ if (onSuccess is null)
+ {
+ throw new ArgumentNullException(nameof(onSuccess));
+ }
+ #endif
- if (onFailure is null)
- {
- throw new ArgumentNullException(nameof(onFailure));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(onFailure);
+ #else
+ if (onFailure is null)
+ {
+ throw new ArgumentNullException(nameof(onFailure));
+ }
+ #endif
if (IsSuccess)
{
@@ -242,10 +278,14 @@ namespace StevanFreeborn.Results
/// Thrown when action is null.
public Result Map(Action action)
{
- if (action is null)
- {
- throw new ArgumentNullException(nameof(action));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(action);
+ #else
+ if (action is null)
+ {
+ throw new ArgumentNullException(nameof(action));
+ }
+ #endif
if (IsSuccess)
{
@@ -262,10 +302,14 @@ namespace StevanFreeborn.Results
/// Thrown when action is null.
public Result Map(Action action)
{
- if (action is null)
- {
- throw new ArgumentNullException(nameof(action));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(action);
+ #else
+ if (action is null)
+ {
+ throw new ArgumentNullException(nameof(action));
+ }
+ #endif
if (IsSuccess)
{
diff --git a/src/StevanFreeborn.Results/ResultAsyncExtensions.cs b/src/StevanFreeborn.Results/ResultAsyncExtensions.cs
index ee5ed71..a081816 100644
--- a/src/StevanFreeborn.Results/ResultAsyncExtensions.cs
+++ b/src/StevanFreeborn.Results/ResultAsyncExtensions.cs
@@ -19,15 +19,23 @@ namespace StevanFreeborn.Results
public static async Task> MapAsync(this Result result, Func> mapper)
where TError : IError
{
- if (result is null)
- {
- throw new ArgumentNullException(nameof(result));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(result);
+ #else
+ if (result is null)
+ {
+ throw new ArgumentNullException(nameof(result));
+ }
+ #endif
- if (mapper is null)
- {
- throw new ArgumentNullException(nameof(mapper));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(mapper);
+ #else
+ if (mapper is null)
+ {
+ throw new ArgumentNullException(nameof(mapper));
+ }
+ #endif
return result.IsSuccess ? await mapper(result.Value).ConfigureAwait(false) : result.Error;
}
@@ -46,15 +54,23 @@ namespace StevanFreeborn.Results
where TError : IError
where TNewError : IError
{
- if (result is null)
- {
- throw new ArgumentNullException(nameof(result));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(result);
+ #else
+ if (result is null)
+ {
+ throw new ArgumentNullException(nameof(result));
+ }
+ #endif
- if (mapper is null)
- {
- throw new ArgumentNullException(nameof(mapper));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(mapper);
+ #else
+ if (mapper is null)
+ {
+ throw new ArgumentNullException(nameof(mapper));
+ }
+ #endif
return result.IsFailure ? await mapper(result.Error).ConfigureAwait(false) : Result.Ok(result.Value);
}
@@ -70,15 +86,23 @@ namespace StevanFreeborn.Results
public static async Task> MapAsync(this Result result, Func onSuccess)
where TError : IError
{
- if (result is null)
- {
- throw new ArgumentNullException(nameof(result));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(result);
+ #else
+ if (result is null)
+ {
+ throw new ArgumentNullException(nameof(result));
+ }
+ #endif
- if (onSuccess is null)
- {
- throw new ArgumentNullException(nameof(onSuccess));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(onSuccess);
+ #else
+ if (onSuccess is null)
+ {
+ throw new ArgumentNullException(nameof(onSuccess));
+ }
+ #endif
if (result.IsSuccess)
{
@@ -99,15 +123,23 @@ namespace StevanFreeborn.Results
public static async Task> BindAsync(this Result result, Func>> onSuccess)
where TError : IError
{
- if (result is null)
- {
- throw new ArgumentNullException(nameof(result));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(result);
+ #else
+ if (result is null)
+ {
+ throw new ArgumentNullException(nameof(result));
+ }
+ #endif
- if (onSuccess is null)
- {
- throw new ArgumentNullException(nameof(onSuccess));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(onSuccess);
+ #else
+ if (onSuccess is null)
+ {
+ throw new ArgumentNullException(nameof(onSuccess));
+ }
+ #endif
return result.IsSuccess ? await onSuccess(result.Value).ConfigureAwait(false) : result;
}
@@ -125,20 +157,32 @@ namespace StevanFreeborn.Results
public static async Task MatchAsync(this Result result, Func> onSuccess, Func> onFailure)
where TError : IError
{
- if (result is null)
- {
- throw new ArgumentNullException(nameof(result));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(result);
+ #else
+ if (result is null)
+ {
+ throw new ArgumentNullException(nameof(result));
+ }
+ #endif
- if (onSuccess is null)
- {
- throw new ArgumentNullException(nameof(onSuccess));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(onSuccess);
+ #else
+ if (onSuccess is null)
+ {
+ throw new ArgumentNullException(nameof(onSuccess));
+ }
+ #endif
- if (onFailure is null)
- {
- throw new ArgumentNullException(nameof(onFailure));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(onFailure);
+ #else
+ if (onFailure is null)
+ {
+ throw new ArgumentNullException(nameof(onFailure));
+ }
+ #endif
return result.IsSuccess
? await onSuccess(result.Value).ConfigureAwait(false)
@@ -164,15 +208,23 @@ namespace StevanFreeborn.Results
/// Thrown when action or errorHandler is null.
public static async Task> TryAsync(Func action, Func errorHandler)
{
- if (action is null)
- {
- throw new ArgumentNullException(nameof(action));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(action);
+ #else
+ if (action is null)
+ {
+ throw new ArgumentNullException(nameof(action));
+ }
+ #endif
- if (errorHandler is null)
- {
- throw new ArgumentNullException(nameof(errorHandler));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(errorHandler);
+ #else
+ if (errorHandler is null)
+ {
+ throw new ArgumentNullException(nameof(errorHandler));
+ }
+ #endif
try
{
@@ -198,15 +250,23 @@ namespace StevanFreeborn.Results
public static async Task> BindAsync(this Result result, Func>> binder)
where TError : IError
{
- if (result is null)
- {
- throw new ArgumentNullException(nameof(result));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(result);
+ #else
+ if (result is null)
+ {
+ throw new ArgumentNullException(nameof(result));
+ }
+ #endif
- if (binder is null)
- {
- throw new ArgumentNullException(nameof(binder));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(binder);
+ #else
+ if (binder is null)
+ {
+ throw new ArgumentNullException(nameof(binder));
+ }
+ #endif
return result.IsSuccess ? await binder(result.Value).ConfigureAwait(false) : Result.Fail(result.Error);
}
@@ -225,20 +285,32 @@ namespace StevanFreeborn.Results
public static async Task MatchAsync(this Result result, Func> onSuccess, Func> onFailure)
where TError : IError
{
- if (result is null)
- {
- throw new ArgumentNullException(nameof(result));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(result);
+ #else
+ if (result is null)
+ {
+ throw new ArgumentNullException(nameof(result));
+ }
+ #endif
- if (onSuccess is null)
- {
- throw new ArgumentNullException(nameof(onSuccess));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(onSuccess);
+ #else
+ if (onSuccess is null)
+ {
+ throw new ArgumentNullException(nameof(onSuccess));
+ }
+ #endif
- if (onFailure is null)
- {
- throw new ArgumentNullException(nameof(onFailure));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(onFailure);
+ #else
+ if (onFailure is null)
+ {
+ throw new ArgumentNullException(nameof(onFailure));
+ }
+ #endif
return result.IsSuccess
? await onSuccess(result.Value).ConfigureAwait(false)
@@ -266,15 +338,23 @@ namespace StevanFreeborn.Results
/// Thrown when func or errorHandler is null.
public static async Task> TryAsync(Func> func, Func errorHandler)
{
- if (func is null)
- {
- throw new ArgumentNullException(nameof(func));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(func);
+ #else
+ if (func is null)
+ {
+ throw new ArgumentNullException(nameof(func));
+ }
+ #endif
- if (errorHandler is null)
- {
- throw new ArgumentNullException(nameof(errorHandler));
- }
+ #if NET6_0_OR_GREATER
+ ArgumentNullException.ThrowIfNull(errorHandler);
+ #else
+ if (errorHandler is null)
+ {
+ throw new ArgumentNullException(nameof(errorHandler));
+ }
+ #endif
try
{