ArgDefender 2.1.1
dotnet add package ArgDefender --version 2.1.1
NuGet\Install-Package ArgDefender -Version 2.1.1
<PackageReference Include="ArgDefender" Version="2.1.1" />
<PackageVersion Include="ArgDefender" Version="2.1.1" />
<PackageReference Include="ArgDefender" />
paket add ArgDefender --version 2.1.1
#r "nuget: ArgDefender, 2.1.1"
#:package ArgDefender@2.1.1
#addin nuget:?package=ArgDefender&version=2.1.1
#tool nuget:?package=ArgDefender&version=2.1.1
ArgDefender
Fluent argument validation for .NET that keeps guard clauses readable and fast.
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 vianameof(). - Optional
securemode 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
ArgumentExceptionand include the parameter name in the message. - Generic comparison guards (
Min,Max,InRange, etc.) throwArgumentOutOfRangeException. Length, collection-count, and URI-port checks throwArgumentException. - Null enforcement (
NotNull,NotAllNull) throwsArgumentNullException.
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 | Versions 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. |
-
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.