Skip to content

Provides a set of backward compatible `throw` helper methods, which have been added in previous .NET versions.

License

Notifications You must be signed in to change notification settings

dailydevops/arguments

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Arguments

License Build Status NuGet NuGet Downloads Contributors

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.

Overview

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

Projects

Core Library

  • 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)

Tests

  • NetEvolve.Arguments.Tests.Unit - Comprehensive unit tests covering all validation methods across all target frameworks

Features

  • 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

Getting Started

Prerequisites

Installation

Add the NuGet package to your project:

dotnet add package NetEvolve.Arguments

Or via Package Manager Console:

Install-Package NetEvolve.Arguments

Or 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.

Usage Examples

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;
}

Development

Building

Clone the repository and build the solution:

git clone https://github.com/dailydevops/arguments.git
cd arguments
dotnet restore
dotnet build

Running Tests

Run all tests across all target frameworks:

dotnet test

Run tests for a specific project:

dotnet test tests/NetEvolve.Arguments.Tests.Unit

Code Formatting

This project uses CSharpier for consistent code formatting:

dotnet csharpier format .

Code formatting is automatically enforced during builds via the CSharpier.MSBuild package.

Project Structure

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 templates

Architecture

This 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:

Contributing

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

Code of Conduct

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.

Documentation

Versioning

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 ! or BREAKING 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.

Support

  • Issues: Report bugs or request features on GitHub Issues
  • Documentation: Read the full documentation in this repository
  • NuGet: Download the package from NuGet.org

License

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.

About

Provides a set of backward compatible `throw` helper methods, which have been added in previous .NET versions.

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Sponsor this project

 

Contributors 5

Languages