|
1 | 1 | # NetEvolve.Arguments |
2 | | -Provides a set of backward compatible argument `throw` helper methods added in the latest .NET versions. |
3 | | -Especially intended for projects with multiple `TargetFrameworks`, for usage, standardization and maintainability. |
4 | 2 |
|
5 | | -## Method Overview |
6 | | -The following methods are currently provided. |
| 3 | +[](LICENSE) |
| 4 | +[](https://github.com/dailydevops/arguments/actions) |
7 | 5 |
|
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**. |
| 6 | +A comprehensive library providing backward-compatible argument validation helper methods (`ThrowIf*`) for .NET projects targeting multiple framework versions. This library enables modern argument validation patterns across legacy and current .NET runtimes, ensuring code consistency and maintainability. |
10 | 7 |
|
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**. |
| 8 | +## Overview |
13 | 9 |
|
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**. |
| 10 | +Modern .NET versions (starting with .NET 6) introduced streamlined argument validation methods such as `ArgumentNullException.ThrowIfNull` and `ArgumentOutOfRangeException.ThrowIfEqual`. However, projects targeting multiple frameworks or older .NET versions cannot utilize these convenient methods without conditional compilation or duplicated validation logic. |
16 | 11 |
|
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**. |
| 12 | +**NetEvolve.Arguments** bridges this gap by providing polyfilled implementations of these modern validation methods, allowing developers to write consistent, maintainable argument validation code regardless of the target framework. |
19 | 13 |
|
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**. |
| 14 | +## Key Features |
22 | 15 |
|
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**. |
| 16 | +- **Multi-Framework Support**: Compatible with .NET Standard 2.0, .NET 8.0, .NET 9.0, and .NET 10.0 |
| 17 | +- **Zero Runtime Overhead**: Uses conditional compilation to delegate to native implementations where available |
| 18 | +- **Drop-in Replacement**: Identical API signatures to native .NET implementations |
| 19 | +- **Type-Safe**: Fully generic implementations with proper type constraints |
| 20 | +- **Comprehensive Coverage**: Includes null checks, range validations, and equality comparisons |
25 | 21 |
|
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**. |
| 22 | +## Installation |
28 | 23 |
|
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**. |
| 24 | +Install the package via NuGet Package Manager: |
31 | 25 |
|
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**. |
| 26 | +```bash |
| 27 | +dotnet add package NetEvolve.Arguments |
| 28 | +``` |
34 | 29 |
|
35 | | -### `Argument.ThrowIfNullOrEmpty<T>(IEnumerable<T>?, string?)` (Individuall extension) |
36 | | -Throws an `ArgumentNullException` if the argument is `null` or throws an `ArgumentException` if the argument is empty. |
| 30 | +Or via the Package Manager Console: |
37 | 31 |
|
38 | | -### `Argument.ThrowIfNullOrWhiteSpace(string?, string?)` |
39 | | -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**. |
| 32 | +```powershell |
| 33 | +Install-Package NetEvolve.Arguments |
| 34 | +``` |
| 35 | + |
| 36 | +## Usage |
| 37 | + |
| 38 | +Import the namespace in your code: |
| 39 | + |
| 40 | +```csharp |
| 41 | +using NetEvolve.Arguments; |
| 42 | +``` |
| 43 | + |
| 44 | +Then use the validation methods just as you would with native .NET implementations: |
| 45 | + |
| 46 | +```csharp |
| 47 | +public void ProcessData(string data, int count) |
| 48 | +{ |
| 49 | + Argument.ThrowIfNullOrWhiteSpace(data); |
| 50 | + Argument.ThrowIfLessThan(count, 1); |
| 51 | + |
| 52 | + // Your implementation |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +## Available Methods |
| 57 | + |
| 58 | +### Null Validation |
| 59 | + |
| 60 | +#### `Argument.ThrowIfNull(object?, string?)` |
| 61 | +Throws an `ArgumentNullException` if the argument is `null`. |
| 62 | + |
| 63 | +**Replacement for**: [`ArgumentNullException.ThrowIfNull(object, string)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentnullexception.throwifnull) (introduced in .NET 6) |
| 64 | + |
| 65 | +**Example**: |
| 66 | +```csharp |
| 67 | +public void Process(object data) |
| 68 | +{ |
| 69 | + Argument.ThrowIfNull(data); |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +#### `Argument.ThrowIfNull(void*, string?)` |
| 74 | +Throws an `ArgumentNullException` if the pointer argument is `null`. |
| 75 | + |
| 76 | +**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)) (introduced in .NET 7) |
| 77 | + |
| 78 | +#### `Argument.ThrowIfNullOrEmpty(string?, string?)` |
| 79 | +Throws an `ArgumentNullException` if the argument is `null`, or an `ArgumentException` if the argument is an empty string. |
| 80 | + |
| 81 | +**Replacement for**: [`ArgumentException.ThrowIfNullOrEmpty(string, string)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentexception.throwifnullorempty) (introduced in .NET 7) |
| 82 | + |
| 83 | +**Example**: |
| 84 | +```csharp |
| 85 | +public void Process(string name) |
| 86 | +{ |
| 87 | + Argument.ThrowIfNullOrEmpty(name); |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +#### `Argument.ThrowIfNullOrEmpty<T>(IEnumerable<T>?, string?)` |
| 92 | +Throws an `ArgumentNullException` if the argument is `null`, or an `ArgumentException` if the collection is empty. |
| 93 | + |
| 94 | +**Note**: This is a custom extension method not present in the base .NET framework, providing convenient collection validation. |
| 95 | + |
| 96 | +**Example**: |
| 97 | +```csharp |
| 98 | +public void Process(IEnumerable<int> items) |
| 99 | +{ |
| 100 | + Argument.ThrowIfNullOrEmpty(items); |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +#### `Argument.ThrowIfNullOrWhiteSpace(string?, string?)` |
| 105 | +Throws an `ArgumentNullException` if the argument is `null`, or an `ArgumentException` if the argument is empty or contains only white-space characters. |
| 106 | + |
| 107 | +**Replacement for**: [`ArgumentException.ThrowIfNullOrWhiteSpace(string, string)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentexception.throwifnullorwhitespace) (introduced in .NET 8) |
| 108 | + |
| 109 | +**Example**: |
| 110 | +```csharp |
| 111 | +public void Process(string description) |
| 112 | +{ |
| 113 | + Argument.ThrowIfNullOrWhiteSpace(description); |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +### Range Validation |
| 118 | + |
| 119 | +#### `Argument.ThrowIfEqual<T>(T, T, string?)` |
| 120 | +Throws an `ArgumentOutOfRangeException` if the first argument is equal to the second argument. |
| 121 | + |
| 122 | +**Replacement for**: [`ArgumentOutOfRangeException.ThrowIfEqual<T>(T, T, string)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception.throwifequal) (introduced in .NET 8) |
| 123 | + |
| 124 | +**Example**: |
| 125 | +```csharp |
| 126 | +public void SetValue(int value) |
| 127 | +{ |
| 128 | + Argument.ThrowIfEqual(value, 0); // Value must not be zero |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +#### `Argument.ThrowIfNotEqual<T>(T, T, string?)` |
| 133 | +Throws an `ArgumentOutOfRangeException` if the first argument is not equal to the second argument. |
| 134 | + |
| 135 | +**Replacement for**: [`ArgumentOutOfRangeException.ThrowIfNotEqual<T>(T, T, string)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception.throwifnotequal) (introduced in .NET 8) |
| 136 | + |
| 137 | +#### `Argument.ThrowIfGreaterThan<T>(T, T, string?)` |
| 138 | +Throws an `ArgumentOutOfRangeException` if the first argument is greater than the second argument. |
| 139 | + |
| 140 | +**Replacement for**: [`ArgumentOutOfRangeException.ThrowIfGreaterThan<T>(T, T, string)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception.throwifgreaterthan) (introduced in .NET 8) |
| 141 | + |
| 142 | +**Example**: |
| 143 | +```csharp |
| 144 | +public void SetAge(int age) |
| 145 | +{ |
| 146 | + Argument.ThrowIfGreaterThan(age, 150); // Age must be 150 or less |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +#### `Argument.ThrowIfGreaterThanOrEqual<T>(T, T, string?)` |
| 151 | +Throws an `ArgumentOutOfRangeException` if the first argument is greater than or equal to the second argument. |
| 152 | + |
| 153 | +**Replacement for**: [`ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual<T>(T, T, string)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception.throwifgreaterthanorequal) (introduced in .NET 8) |
| 154 | + |
| 155 | +**Example**: |
| 156 | +```csharp |
| 157 | +public void SetCount(int count, int maximum) |
| 158 | +{ |
| 159 | + Argument.ThrowIfGreaterThanOrEqual(count, maximum); // Count must be less than maximum |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +#### `Argument.ThrowIfLessThan<T>(T, T, string?)` |
| 164 | +Throws an `ArgumentOutOfRangeException` if the first argument is less than the second argument. |
| 165 | + |
| 166 | +**Replacement for**: [`ArgumentOutOfRangeException.ThrowIfLessThan<T>(T, T, string)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception.throwiflessthan) (introduced in .NET 8) |
| 167 | + |
| 168 | +**Example**: |
| 169 | +```csharp |
| 170 | +public void SetCount(int count) |
| 171 | +{ |
| 172 | + Argument.ThrowIfLessThan(count, 1); // Count must be at least 1 |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +#### `Argument.ThrowIfLessThanOrEqual<T>(T, T, string?)` |
| 177 | +Throws an `ArgumentOutOfRangeException` if the first argument is less than or equal to the second argument. |
| 178 | + |
| 179 | +**Replacement for**: [`ArgumentOutOfRangeException.ThrowIfLessThanOrEqual<T>(T, T, string)`](https://learn.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception.throwiflessthanorequal) (introduced in .NET 8) |
| 180 | + |
| 181 | +**Example**: |
| 182 | +```csharp |
| 183 | +public void SetMinimum(int value, int threshold) |
| 184 | +{ |
| 185 | + Argument.ThrowIfLessThanOrEqual(value, threshold); // Value must be greater than threshold |
| 186 | +} |
| 187 | +``` |
| 188 | + |
| 189 | +## Framework Compatibility |
| 190 | + |
| 191 | +| Target Framework | Status | Notes | |
| 192 | +|-----------------|--------|-------| |
| 193 | +| .NET Standard 2.0 | ✅ Supported | Full polyfill implementations | |
| 194 | +| .NET 8.0 | ✅ Supported | Delegates to native implementations where available | |
| 195 | +| .NET 9.0 | ✅ Supported | Full native delegation | |
| 196 | +| .NET 10.0 | ✅ Supported | Full native delegation | |
| 197 | + |
| 198 | +## Contributing |
| 199 | + |
| 200 | +Contributions are welcome! Please feel free to submit issues, fork the repository, and create pull requests. |
| 201 | + |
| 202 | +## License |
| 203 | + |
| 204 | +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. |
| 205 | + |
| 206 | +## Related Resources |
| 207 | + |
| 208 | +- [Official .NET API Documentation](https://learn.microsoft.com/en-us/dotnet/api/) |
| 209 | +- [Argument Validation Best Practices](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/exceptions/) |
| 210 | +- [Project Repository](https://github.com/dailydevops/arguments) |
0 commit comments