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" />
<PackageReference Include="ArgDefender" />
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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
#tool nuget:?package=ArgDefender&version=2.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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.
- 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 vianameof(). - Optional
securemode 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
ArgumentExceptionand include the parameter name in the message. - Range/comparison guards (
Min,Max,InRange, etc.) throwArgumentOutOfRangeException. - Null enforcement (
NotNull,NotAllNull) throwsArgumentNullException.
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 | 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. |
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.