A universal polyfill library that provides modern ArgumentNullException.ThrowIf* and ArgumentException.ThrowIf* helper methods across all .NET runtimes (.NET Standard 2.0+, .NET Framework 4.6.2+, .NET 6.0+), enabling consistent argument validation patterns regardless of target framework version.
NetEvolve.Arguments brings modern .NET argument validation APIs to legacy frameworks, allowing you to write consistent defensive programming code across all supported .NET platforms. The library polyfills the ThrowIfNull, ThrowIfNullOrEmpty, ThrowIfNullOrWhiteSpace, and comparison-based validation methods that were introduced in later .NET versions, making them available to projects targeting older frameworks.
This solution provides a single, focused library designed to:
- Enable modern code patterns: Write modern argument validation code that works on .NET Framework 4.6.2 through .NET 10.0
- Maintain consistency: Use the same API surface across all target frameworks without conditional compilation
- Simplify maintenance: Replace verbose manual null checks and validation logic with concise, expressive helper methods
- Improve code quality: Apply defensive programming practices consistently throughout your codebase
- NetEvolve.Arguments - The main polyfill library providing argument validation helper methods for:
- Null checking (
ThrowIfNull) - Empty/whitespace validation (
ThrowIfNullOrEmpty,ThrowIfNullOrWhiteSpace) - Equality validation (
ThrowIfEqual,ThrowIfNotEqual) - Comparison validation (
ThrowIfGreaterThan,ThrowIfGreaterThanOrEqual,ThrowIfLessThan,ThrowIfLessThanOrEqual) - Object disposal validation (
ObjectDisposedException.ThrowIf)
- Null checking (
- NetEvolve.Arguments.Tests.Unit - Comprehensive unit tests covering all validation methods across all target frameworks
- Universal compatibility - Supports .NET Standard 2.0+, .NET Framework 4.6.2, 4.7.2, 4.8, 4.8.1, and .NET 6.0 through 10.0
- Modern API surface - Provides the same helper methods available in .NET 6+ to all target frameworks
- Zero overhead - On frameworks where native implementations exist, the polyfills are compiled out
- Type-safe validation - Generic and specialized overloads for common scenarios including pointers and spans
- Performance optimized - Minimal allocations and optimized code paths for each target framework
- Comprehensive testing - Extensive unit test coverage across all supported frameworks
- .NET SDK 10.0 or higher for building the solution
- Git for version control
- Visual Studio 2022 or Visual Studio Code (recommended)
Add the NuGet package to your project:
dotnet add package NetEvolve.ArgumentsOr via Package Manager Console:
Install-Package NetEvolve.ArgumentsOr add directly to your .csproj file:
<ItemGroup>
<PackageReference Include="NetEvolve.Arguments" />
</ItemGroup>Note
This project uses Centralized Package Version Management. When adding the package reference, omit the Version attribute - versions are managed centrally in Directory.Packages.props.
Null validation:
using System;
public void ProcessData(string data)
{
ArgumentNullException.ThrowIfNull(data);
// Process data safely
}Empty/whitespace validation:
using System;
public void ValidateInput(string input)
{
ArgumentException.ThrowIfNullOrEmpty(input);
ArgumentException.ThrowIfNullOrWhiteSpace(input);
// Input is guaranteed to have content
}Comparison validation:
using System;
public void SetTimeout(int milliseconds)
{
ArgumentOutOfRangeException.ThrowIfNegative(milliseconds);
ArgumentOutOfRangeException.ThrowIfGreaterThan(milliseconds, 60000);
// Timeout is between 0 and 60000
}Object disposal validation:
using System;
public class MyDisposable : IDisposable
{
private bool _disposed;
public void DoWork()
{
ObjectDisposedException.ThrowIf(_disposed, this);
// Safe to perform work
}
public void Dispose() => _disposed = true;
}Clone the repository and build the solution:
git clone https://github.com/dailydevops/arguments.git
cd arguments
dotnet restore
dotnet buildRun all tests across all target frameworks:
dotnet testRun tests for a specific project:
dotnet test tests/NetEvolve.Arguments.Tests.UnitThis project uses CSharpier for consistent code formatting:
dotnet csharpier format .Code formatting is automatically enforced during builds via the CSharpier.MSBuild package.
src/
└── NetEvolve.Arguments/ # Main polyfill library
├── Argument*.cs # Obsolete implementations
├── ArgumentExceptionPolyfill.cs # Polyfill implementations
├── ArgumentNullExceptionPolyfills.cs # Polyfill implementations
├── ArgumentOutOfRangeExceptionPolyfills.cs # Polyfill implementations
└── ObjectDisposedExceptionPolyfills.cs # Polyfill implementations
tests/
└── NetEvolve.Arguments.Tests.Unit/ # Comprehensive unit tests
decisions/ # Architecture Decision Records (ADRs)
templates/ # Documentation templatesThis library follows a polyfill architecture pattern:
- Conditional compilation: Uses preprocessor directives to provide native implementations where available and polyfills where needed
- Framework detection: Automatically detects target framework capabilities at compile time
- Zero-overhead abstraction: On modern frameworks, the polyfills compile to simple pass-through calls or are eliminated entirely
- Backward compatibility: Ensures older frameworks receive functionally equivalent implementations
For detailed architectural decisions, see:
- Centralized Package Version Management
- Conventional Commits
- GitVersion for Automated Semantic Versioning
- .NET 10 and C# 13 Adoption
- English as Project Language
- DateTimeOffset and TimeProvider Usage
- All Architecture Decision Records
We welcome contributions from the community! Please read our Contributing Guidelines before submitting a pull request.
Key points:
- Follow the Conventional Commits format for commit messages
- Write tests for new functionality across all relevant target frameworks
- Follow existing code style and conventions (enforced by analyzers and CSharpier)
- Update documentation as needed
- Ensure all tests pass on all target frameworks before submitting
This project adheres to the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code. Please report unacceptable behavior to info@daily-devops.net.
- Architecture Decision Records - Detailed architectural decisions and rationale
- Contributing Guidelines - How to contribute to this project
- Code of Conduct - Community standards and expectations
- License - Project licensing information (MIT License)
- Release Notes - Version history and changelog
This project uses GitVersion for automated semantic versioning based on Git history and Conventional Commits. Version numbers are automatically calculated during the build process:
- Major version (1.0.0 → 2.0.0): Breaking changes indicated by
!orBREAKING CHANGE:footer in commit messages - Minor version (1.0.0 → 1.1.0): New features added via
feat:commits - Patch version (1.0.0 → 1.0.1): Bug fixes and maintenance via
fix:,chore:,docs:, etc.
For more details, see GitVersion for Automated Semantic Versioning.
- Issues: Report bugs or request features on GitHub Issues
- Documentation: Read the full documentation in this repository
- NuGet: Download the package from NuGet.org
This project is licensed under the MIT License - see the LICENSE file for details.
Note
Made with ❤️ by the NetEvolve Team
Visit us at https://www.daily-devops.net for more information about our services and solutions.