VOOZH about

URL: https://www.nuget.org/packages/Guard.Net/

⇱ NuGet Gallery | Guard.Net 4.0.0




Guard.Net 4.0.0

dotnet add package Guard.Net --version 4.0.0
 
 
NuGet\Install-Package Guard.Net -Version 4.0.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="Guard.Net" Version="4.0.0" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Guard.Net" Version="4.0.0" />
 
Directory.Packages.props
<PackageReference Include="Guard.Net" />
 
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 Guard.Net --version 4.0.0
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Guard.Net, 4.0.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 Guard.Net@4.0.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=Guard.Net&version=4.0.0
 
Install as a Cake Addin
#tool nuget:?package=Guard.Net&version=4.0.0
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Guard.NET

👁 NuGet
👁 NuGet Downloads
👁 License: MIT

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:

  1. Standard — throws a framework exception with the parameter name and an optional message.
  2. Custom message — same as above, but with your own error message.
  3. 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 net5.0 was computed.  net5.0-windows net5.0-windows was computed.  net6.0 net6.0 was computed.  net6.0-android net6.0-android was computed.  net6.0-ios net6.0-ios was computed.  net6.0-maccatalyst net6.0-maccatalyst was computed.  net6.0-macos net6.0-macos was computed.  net6.0-tvos net6.0-tvos was computed.  net6.0-windows net6.0-windows was computed.  net7.0 net7.0 was computed.  net7.0-android net7.0-android was computed.  net7.0-ios net7.0-ios was computed.  net7.0-maccatalyst net7.0-maccatalyst was computed.  net7.0-macos net7.0-macos was computed.  net7.0-tvos net7.0-tvos was computed.  net7.0-windows net7.0-windows was computed.  net8.0 net8.0 is compatible.  net8.0-android net8.0-android was computed.  net8.0-browser net8.0-browser was computed.  net8.0-ios net8.0-ios was computed.  net8.0-maccatalyst net8.0-maccatalyst was computed.  net8.0-macos net8.0-macos was computed.  net8.0-tvos net8.0-tvos was computed.  net8.0-windows net8.0-windows was computed.  net9.0 net9.0 was computed.  net9.0-android net9.0-android was computed.  net9.0-browser net9.0-browser was computed.  net9.0-ios net9.0-ios was computed.  net9.0-maccatalyst net9.0-maccatalyst was computed.  net9.0-macos net9.0-macos was computed.  net9.0-tvos net9.0-tvos was computed.  net9.0-windows net9.0-windows was computed.  net10.0 net10.0 is compatible.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
.NET Core netcoreapp3.0 netcoreapp3.0 was computed.  netcoreapp3.1 netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 netstandard2.1 is compatible. 
MonoAndroid monoandroid monoandroid was computed. 
MonoMac monomac monomac was computed. 
MonoTouch monotouch monotouch was computed. 
Tizen tizen60 tizen60 was computed. 
Xamarin.iOS xamarinios xamarinios was computed. 
Xamarin.Mac xamarinmac xamarinmac was computed. 
Xamarin.TVOS xamarintvos xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .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.
Version Downloads Last Updated
4.0.0 1,660 4/11/2026
3.0.0 929,078 8/26/2022
2.0.0 516,018 10/5/2021
1.2.0 813,436 8/24/2018
1.0.1 1,707 8/23/2018
1.0.0.2 6,204 6/25/2018
1.0.0.1 1,993 6/19/2018
1.0.0 21,748 4/10/2018

Migrated to .NET 10 and .NET 8 LTS. Dropped support for .NET 6 and .NET Core 3.1.