Guard.Net
4.0.0
dotnet add package Guard.Net --version 4.0.0
NuGet\Install-Package Guard.Net -Version 4.0.0
<PackageReference Include="Guard.Net" Version="4.0.0" />
<PackageVersion Include="Guard.Net" Version="4.0.0" />
<PackageReference Include="Guard.Net" />
paket add Guard.Net --version 4.0.0
#r "nuget: Guard.Net, 4.0.0"
#:package Guard.Net@4.0.0
#addin nuget:?package=Guard.Net&version=4.0.0
#tool nuget:?package=Guard.Net&version=4.0.0
Guard.NET
A simple, zero-dependency library that facilitates runtime checks of code and allows you to define preconditions and invariants within a method.
Its main purpose is to leverage the precondition checks that appear in almost all methods, through a clean interface that accentuates intention and eliminates confusion.
Supported targets: .NET 10, .NET 8, .NET Standard 2.1
Installation
Install-Package Guard.NET
Or via the .NET CLI:
dotnet add package Guard.NET
Quick Start
public void AddUser(User user)
{
Guard.NotNull(user, nameof(user));
// ...
}
Usage
Every guard method supports three overload patterns:
- Standard — throws a framework exception with the parameter name and an optional message.
- Custom message — same as above, but with your own error message.
- Custom exception — throws any exception type you provide.
Null Checks
// throws ArgumentNullException
Guard.NotNull(user, nameof(user));
Guard.NotNull(user, nameof(user), "User must be provided");
// throws a custom exception
Guard.NotNull(user, new InvalidOperationException("No user"));
String Checks
// throws ArgumentException when null or empty
Guard.NotNullOrEmpty(name, nameof(name));
Guard.NotNullOrEmpty(name, nameof(name), "Name is required");
// throws ArgumentException when null, empty, or whitespace
Guard.NotNullOrWhitespace(name, nameof(name));
Guard.NotNullOrWhitespace(name, nameof(name), "Name cannot be blank");
Range Checks
All range checks work with any type that implements IComparable<T>.
// throws ArgumentOutOfRangeException
Guard.NotGreaterThan(pageSize, maxPageSize, nameof(pageSize));
Guard.NotGreaterThanOrEqualTo(index, count, nameof(index));
Guard.NotLessThan(age, 0, nameof(age));
Guard.NotLessThanOrEqualTo(quantity, 0, nameof(quantity));
Equality Checks
// throws a custom exception when values are equal
Guard.NotEqualTo<int, ArgumentException>(status, 0);
Guard.NotEqualTo(status, 0, new InvalidOperationException("Status must be set"));
Collection Checks
// throws ArgumentException when the collection is empty
Guard.NotAny(items, nameof(items));
Guard.NotAny(items, nameof(items), "At least one item is required");
Regex Checks
// throws ArgumentException when the pattern does not match
Guard.NotIsMatch(email, nameof(email), @"^[\w.-]+@[\w.-]+\.\w+$");
Guard.NotIsMatch(email, nameof(email), @"^[\w.-]+@[\w.-]+\.\w+$", "Invalid email format");
Custom Predicate
For any condition not covered by the built-in methods:
Guard.For<ArgumentException>(() => userId < 0);
Guard.For<ArgumentException>(() => userId < 0, "User ID cannot be negative");
Guard.For(() => userId < 0, new ArgumentException(nameof(userId)));
Custom Exception Types
All guard methods have generic overloads that let you throw any exception type:
Guard.NotNull<User, InvalidOperationException>(user);
Guard.NotNullOrEmpty<InvalidOperationException>(name, "Name is required");
Guard.NotGreaterThan<int, BusinessRuleException>(pageSize, maxPageSize);
Guard.NotAny<Order, ValidationException>(orders, "Order list cannot be empty");
API Reference
| Method | Default Exception | Description |
|---|---|---|
NotNull |
ArgumentNullException |
Ensures a reference is not null |
NotNullOrEmpty |
ArgumentException |
Ensures a string is not null or empty |
NotNullOrWhitespace |
ArgumentException |
Ensures a string is not null, empty, or whitespace |
NotGreaterThan |
ArgumentOutOfRangeException |
Ensures a value does not exceed a threshold |
NotGreaterThanOrEqualTo |
ArgumentOutOfRangeException |
Ensures a value is strictly less than a threshold |
NotLessThan |
ArgumentOutOfRangeException |
Ensures a value is not below a threshold |
NotLessThanOrEqualTo |
ArgumentOutOfRangeException |
Ensures a value is strictly greater than a threshold |
NotEqualTo |
(custom only) | Ensures two values are not equal |
NotAny |
ArgumentException |
Ensures a collection is not empty |
NotIsMatch |
ArgumentException |
Ensures a string matches a regex pattern |
For |
(custom only) | Evaluates a predicate and throws if true |
License
This project is licensed under the MIT License.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. 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. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- No dependencies.
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages (9)
Showing the top 5 NuGet packages that depend on Guard.Net:
| Package | Downloads |
|---|---|
|
Arcus.Observability.Telemetry.Serilog.Enrichers
Provides capability to improve telemetry with Serilog in applications |
|
|
Arcus.Security.Core
Contains core functionality for Arcus.Security |
|
|
Arcus.Observability.Telemetry.AzureFunctions
Provides capability to improve telemetry with Serilog in Azure Functions applications |
|
|
Arcus.Observability.Telemetry.Serilog.Filters
Provides capability to reduce telemetry with Serilog filters |
|
|
csharp-xpx-chain-sdk
ProximaX Sirius Blockchain .Net SDK |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on Guard.Net:
| Repository | Stars |
|---|---|
|
tomkerkhove/promitor
Bringing Azure Monitor metrics where you need them.
|
Migrated to .NET 10 and .NET 8 LTS. Dropped support for .NET 6 and .NET Core 3.1.