ArgDefender 2.1.0

dotnet add package ArgDefender --version 2.1.0
                    
NuGet\Install-Package ArgDefender -Version 2.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ArgDefender" Version="2.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ArgDefender" Version="2.1.0" />
                    
Directory.Packages.props
<PackageReference Include="ArgDefender" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ArgDefender --version 2.1.0
                    
#r "nuget: ArgDefender, 2.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ArgDefender@2.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ArgDefender&version=2.1.0
                    
Install as a Cake Addin
#tool nuget:?package=ArgDefender&version=2.1.0
                    
Install as a Cake Tool

ArgDefender

Fluent argument validation for .NET that keeps guard clauses readable and fast.

NuGet Release codecov

Why ArgDefender?

Fail fast: validate inputs at your code boundaries so problems surface immediately, with clear exceptions to pinpoint the offending argument.

  • Fluent, chainable guards with clear exception types.
  • Null-tolerant for nullable inputs; add NotNull() when you need to enforce non-null.
  • Automatic parameter names; Guard.Argument(value) captures the name so you do not need to pass strings via nameof().
  • Optional secure mode redacts sensitive values from exception messages.

Installation

dotnet add package ArgDefender

Supported frameworks

  • net10

Quick start

using ArgDefender;

public sealed class Person
{
    public Person(string name, int age, Uri homepage)
    {
        Guard.Argument(name).NotNull().NotWhiteSpace();
        Guard.Argument(age).Min(0).Max(130);
        Guard.Argument(homepage).UriAbsolute();

        Name = name;
        Age = age;
        Homepage = homepage;
    }

    public string Name { get; }
    public int Age { get; }
    public Uri Homepage { get; }
}

Behavior

ArgDefender throws standard exceptions with consistent, predefined messages:

  • Most guards throw ArgumentException and include the parameter name in the message.
  • Range/comparison guards (Min, Max, InRange, etc.) throw ArgumentOutOfRangeException.
  • Null enforcement (NotNull, NotAllNull) throws ArgumentNullException.

For checks that would otherwise echo values, secure: true replaces the message with "<name> is invalid.".

Examples

Guard chaining

Guard.Argument(name).NotNull().NotEmpty().MaxLength(200);

Custom messages

Guard.Argument(port).InRange(1, 65535, (value, min, max) => $"Port must be {min}-{max}.");

Nullable inputs

string? name = request.Name;
Guard.Argument(name).NotNull().NotWhiteSpace().MaxLength(40);

Secure mode

Guard.Argument(apiKey, secure: true).NotNull().NotWhiteSpace().MinLength(32);

URI checks

var homepage = new Uri("https://example.com");
var redirectUri = new Uri("/callback", UriKind.Relative);
Guard.Argument(homepage).UriHttps();
Guard.Argument(redirectUri).UriRelative();

Enum checks

var status = Status.Pending;
var permissions = Permissions.Read | Permissions.Write;
Guard.Argument(status).EnumDefined();
Guard.Argument(permissions).EnumHasFlag(Permissions.Read);

Defaults and negatives

Guard.Argument(command.Number).NotDefault().NotNegative();

For the detailed list, see docs/standard-validations.md.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.0 666 1/24/2026
1.1.0 11,323 10/24/2023