ArgDefender 2.1.1

dotnet add package ArgDefender --version 2.1.1
                    
NuGet\Install-Package ArgDefender -Version 2.1.1
                    
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.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ArgDefender" Version="2.1.1" />
                    
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.1
                    
#r "nuget: ArgDefender, 2.1.1"
                    
#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.1
                    
#: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.1
                    
Install as a Cake Addin
#tool nuget:?package=ArgDefender&version=2.1.1
                    
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.
  • Most guards tolerate null inputs; add NotNull() when you need to enforce non-null. See null handling for exceptions.
  • Automatic parameter names; Guard.Argument(value) captures the name so you do not need to pass strings via nameof().
  • Optional secure mode redacts values from selected default messages (see below).

Installation

dotnet add package ArgDefender

Supported frameworks

  • .NET 10 (net10.0)

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).NotNull().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.
  • Generic comparison guards (Min, Max, InRange, etc.) throw ArgumentOutOfRangeException. Length, collection-count, and URI-port checks throw ArgumentException.
  • Null enforcement (NotNull, NotAllNull) throws ArgumentNullException.

secure: true replaces default messages with "<name> is invalid." for equality, string matching, collection membership, enum flag, and email host checks. For generic comparison guards, it also omits ArgumentOutOfRangeException.ActualValue. It does not redact every guard message or custom message callback.

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

using ArgDefender;

var apiKey = "demo-invalid-key";
foreach (var secure in new[] { false, true })
{
    try
    {
        Guard.Argument(apiKey, secure: secure).Equal("demo-expected-key");
    }
    catch (ArgumentException exception)
    {
        Console.WriteLine(exception.Message);
    }
}

The default message text is apiKey must be demo-expected-key. without secure mode and apiKey is invalid. with it. The framework appends the parameter name to exception.Message (for example, (Parameter 'apiKey')).

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.1 78 9/16/2026
2.1.0 4,775 1/24/2026
1.1.0 12,022 10/24/2023