Skip to content

Commit d365062

Browse files
authored
chore: Added methods ThrowIfEqual and ThrowIfNotEqual (#128)
* chore: Added methods `ThrowIfEqual` and `ThrowIfNotEqual` * docs: Updated README * chore(tests): Tests for newly introduced methods added * chore(tests): Tests for newly introduced method `ThrowIfNull(void*, string?)` added * docs: Updated README
1 parent 8b3be74 commit d365062

15 files changed

+259
-25
lines changed

README.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,32 @@ Especially intended for projects with multiple `TargetFrameworks`, for usage, st
55
## Method Overview
66
The following methods are currently provided.
77

8-
### ThrowIfGreaterThan
9-
Compatibility method to [`ArgumentOutOfRangeException.ThrowIfGreaterThan<T>(T, T, String)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception.throwifgreaterthan), which was introduced with .NET 8
8+
### `Argument.ThrowIfEqual<T>(T, T, string?)`
9+
Throws an `ArgumentOutOfRangeException` if the first argument is equal to the second argument. Inplace replacement for [`ArgumentOutOfRangeException.ThrowIfEqual<T>(T, T, string)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception.throwifequal), which was introduced with **.NET 8**.
1010

11-
### ThrowIfGreaterThanOrEqual
12-
Compatibility method to [`ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual<T>(T, T, String)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception.throwifgreaterthanorequal), which is part of the framework since .NET 8.
11+
### `Argument.ThrowIfGreaterThan<T>(T, T, string?)`
12+
Throws an `ArgumentOutOfRangeException` if the first argument is greater than the second argument. Inplace replacement for [`ArgumentOutOfRangeException.ThrowIfGreaterThan<T>(T, T, string)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception.throwifgreaterthan), which was introduced with **.NET 8**.
1313

14-
### ThrowIfLessThan
15-
Compatibility method to [`ArgumentOutOfRangeException.ThrowIfLessThan<T>(T, T, String)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception.throwiflessthan), which is part of the framework since .NET 8.
14+
### `Argument.ThrowIfGreaterThanOrEqual<T>(T, T, string?)`
15+
Throws an `ArgumentOutOfRangeException` if the first argument is greater than or equal to the second argument. Inplace replacement for [`ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual<T>(T, T, string)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception.throwifgreaterthanorequal), which was introduced with **.NET 8**.
1616

17-
### ThrowIfLessThanOrEqual
18-
Compatibility method to [`ArgumentOutOfRangeException.ThrowIfLessThanOrEqual<T>(T, T, String)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception.throwiflessthanorequal), which is part of the framework since .NET 8.
17+
### `Argument.ThrowIfLessThan<T>(T, T, string?)`
18+
Throws an `ArgumentOutOfRangeException` if the first argument is less than the second argument. Inplace replacement for [`ArgumentOutOfRangeException.ThrowIfLessThan<T>(T, T, string)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception.throwiflessthan), which was introduced with **.NET 8**.
1919

20-
### ThrowIfNull
21-
Compatibility method to [`ArgumentNullException.ThrowIfNull(Object, String)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentnullexception.throwifnull#system-argumentnullexception-throwifnull(system-object-system-string)), which is part of the framework since .NET 8.
20+
### `Argument.ThrowIfLessThanOrEqual<T>(T, T, string?)`
21+
Throws an `ArgumentOutOfRangeException` if the first argument is less than or equal to the second argument. Inplace replacement for [`ArgumentOutOfRangeException.ThrowIfLessThanOrEqual<T>(T, T, string)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception.throwiflessthanorequal), which was introduced with **.NET 8**.
2222

23-
### ThrowIfNullOrEmpty
24-
Compatibility method to [`ArgumentException.ThrowIfNullOrEmpty(String, String)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentexception.throwifnullorempty), which is part of the framework since .NET 8.
23+
### `Argument.ThrowIfNotEqual<T>(T, T, string?)`
24+
Throws an `ArgumentOutOfRangeException` if the first argument is not equal to the second argument. Inplace replacement for [`ArgumentOutOfRangeException.ThrowIfNotEqual<T>(T, T, string)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception.throwifnotequal), which was introduced with **.NET 8**.
2525

26-
### ThrowIfNullOrWhiteSpace
27-
Compatibility method to [`ArgumentException.ThrowIfNullOrWhiteSpace(String, String)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentexception.throwifnullorwhitespace), which is part of the framework since .NET 8.
26+
### `Argument.ThrowIfNull(object?, string?)`
27+
Throws an `ArgumentNullException` if the argument is `null`. Inplace replacement for [`ArgumentNullException.ThrowIfNull(object, string)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentnullexception.throwifnull), which was introduced with **.NET 6**.
28+
29+
### `Argument.ThrowIfNull(void*, string?)`
30+
Throws an `ArgumentNullException` if the argument is `null`. Inplace replacement for [`ArgumentNullException.ThrowIfNull(void*, string)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentnullexception.throwifnull?view=net-8.0#system-argumentnullexception-throwifnull(system-void*-system-string), which was introduced with **.NET 7**.
31+
32+
### `Argument.ThrowIfNullOrEmpty(string?, string?)`
33+
Throws an `ArgumentNullException` if the argument is `null` or throws an `ArgumentException` if the argument is empty. Inplace replacement for [`ArgumentException.ThrowIfNullOrEmpty(string, string)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentexception.throwifnullorempty), which was introduced with **.NET 7**.
34+
35+
### `Argument.ThrowIfNullOrWhiteSpace(string?, string?)`
36+
Throws an `ArgumentNullException` if the argument is `null` or throws an `ArgumentException` if the argument is empty or contains only white-space characters. Inplace replacement for [`ArgumentException.ThrowIfNullOrWhiteSpace(string, string)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentexception.throwifnullorwhitespace), which was introduced with **.NET 8**.

src/NetEvolve.Arguments/Argument_ThrowArgumentException.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public static partial class Argument
1212
[DoesNotReturn]
1313
[MethodImpl(MethodImplOptions.NoInlining)]
1414
[StackTraceHidden]
15-
private static void ThrowArgumentException(string? paramName)
15+
private static void ThrowArgumentException(string? paramName, string? message = null)
1616
{
17-
throw new ArgumentException(null, paramName);
17+
throw new ArgumentException(message, paramName);
1818
}
1919
}
2020
#endif

src/NetEvolve.Arguments/Argument_ThrowArgumentNullException.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public static partial class Argument
1212
[DoesNotReturn]
1313
[MethodImpl(MethodImplOptions.NoInlining)]
1414
[StackTraceHidden]
15-
private static void ThrowArgumentNullException(string? paramName)
15+
private static void ThrowArgumentNullException(string? paramName, string? message = null)
1616
{
17-
throw new ArgumentNullException(paramName);
17+
throw new ArgumentNullException(paramName, message);
1818
}
1919
}
2020
#endif

src/NetEvolve.Arguments/Argument_ThrowArgumentOutOfRangeException.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ public static partial class Argument
1212
[DoesNotReturn]
1313
[MethodImpl(MethodImplOptions.NoInlining)]
1414
[StackTraceHidden]
15-
private static void ThrowArgumentOutOfRangeException<T>(string? paramName, T value)
16-
where T : IComparable<T>
15+
private static void ThrowArgumentOutOfRangeException<T>(
16+
string? paramName,
17+
T value,
18+
string? message = null
19+
)
1720
{
18-
throw new ArgumentOutOfRangeException(paramName, value, null);
21+
throw new ArgumentOutOfRangeException(paramName, value, message);
1922
}
2023
}
2124
#endif
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace NetEvolve.Arguments;
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Diagnostics;
6+
using System.Runtime.CompilerServices;
7+
8+
public static partial class Argument
9+
{
10+
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is equal to <paramref name="other"/>.</summary>
11+
/// <param name="value">The argument to validate as not equal to <paramref name="other"/>.</param>
12+
/// <param name="other">The value to compare with <paramref name="value"/>.</param>
13+
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
14+
[DebuggerStepThrough]
15+
[StackTraceHidden]
16+
#if NET8_0_OR_GREATER
17+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
18+
#else
19+
[MethodImpl(MethodImplOptions.NoInlining)]
20+
#endif
21+
public static void ThrowIfEqual<T>(
22+
T value,
23+
T other,
24+
[CallerArgumentExpression(nameof(value))] string? paramName = null
25+
)
26+
where T : IEquatable<T>
27+
{
28+
#if NET8_0_OR_GREATER
29+
ArgumentOutOfRangeException.ThrowIfEqual(value, other, paramName);
30+
#else
31+
if (EqualityComparer<T>.Default.Equals(value, other))
32+
{
33+
ThrowArgumentOutOfRangeException(
34+
paramName,
35+
value,
36+
$"{paramName} ('{value}') must not be equal to '{other}'."
37+
);
38+
}
39+
#endif
40+
}
41+
}

src/NetEvolve.Arguments/Argument_ThrowIfGreaterThan.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Diagnostics;
55
using System.Diagnostics.CodeAnalysis;
6+
using System.Globalization;
67
using System.Runtime.CompilerServices;
78

89
public static partial class Argument
@@ -30,7 +31,11 @@ public static void ThrowIfGreaterThan<T>(
3031
#else
3132
if (value.CompareTo(other) > 0)
3233
{
33-
ThrowArgumentOutOfRangeException(paramName, value);
34+
ThrowArgumentOutOfRangeException(
35+
paramName,
36+
value,
37+
$"{paramName} ('{value}') must be less than or equal to '{other}'."
38+
);
3439
}
3540
#endif
3641
}

src/NetEvolve.Arguments/Argument_ThrowIfGreaterThanOrEqual.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ public static void ThrowIfGreaterThanOrEqual<T>(
2929
#else
3030
if (value.CompareTo(other) >= 0)
3131
{
32-
ThrowArgumentOutOfRangeException(paramName, value);
32+
ThrowArgumentOutOfRangeException(
33+
paramName,
34+
value,
35+
$"{paramName} ('{value}') must be less than '{other}'."
36+
);
3337
}
3438
#endif
3539
}

src/NetEvolve.Arguments/Argument_ThrowIfLessThan.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ public static void ThrowIfLessThan<T>(
2929
#else
3030
if (value.CompareTo(other) < 0)
3131
{
32-
ThrowArgumentOutOfRangeException(paramName, value);
32+
ThrowArgumentOutOfRangeException(
33+
paramName,
34+
value,
35+
$"{paramName} ('{value}') must be greater than '{other}'."
36+
);
3337
}
3438
#endif
3539
}

src/NetEvolve.Arguments/Argument_ThrowIfLessThanOrEqual.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ public static void ThrowIfLessThanOrEqual<T>(
2929
#else
3030
if (value.CompareTo(other) <= 0)
3131
{
32-
ThrowArgumentOutOfRangeException(paramName, value);
32+
ThrowArgumentOutOfRangeException(
33+
paramName,
34+
value,
35+
$"{paramName} ('{value}') must be greater than or equal to '{other}'."
36+
);
3337
}
3438
#endif
3539
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace NetEvolve.Arguments;
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Diagnostics;
6+
using System.Runtime.CompilerServices;
7+
8+
public static partial class Argument
9+
{
10+
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is not equal to <paramref name="other"/>.</summary>
11+
/// <param name="value">The argument to validate as equal to <paramref name="other"/>.</param>
12+
/// <param name="other">The value to compare with <paramref name="value"/>.</param>
13+
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
14+
[DebuggerStepThrough]
15+
[StackTraceHidden]
16+
#if NET8_0_OR_GREATER
17+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
18+
#else
19+
[MethodImpl(MethodImplOptions.NoInlining)]
20+
#endif
21+
public static void ThrowIfNotEqual<T>(
22+
T value,
23+
T other,
24+
[CallerArgumentExpression(nameof(value))] string? paramName = null
25+
)
26+
where T : IEquatable<T>
27+
{
28+
#if NET8_0_OR_GREATER
29+
ArgumentOutOfRangeException.ThrowIfNotEqual(value, other, paramName);
30+
#else
31+
if (!EqualityComparer<T>.Default.Equals(value, other))
32+
{
33+
ThrowArgumentOutOfRangeException(
34+
paramName,
35+
value,
36+
$"{paramName} ('{value}') must be equal to '{other}'."
37+
);
38+
}
39+
#endif
40+
}
41+
}

0 commit comments

Comments
 (0)