Guard.NET 1.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Guard.NET --version 1.2.0                
NuGet\Install-Package Guard.NET -Version 1.2.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="1.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Guard.NET --version 1.2.0                
#r "nuget: Guard.NET, 1.2.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.
// Install Guard.NET as a Cake Addin
#addin nuget:?package=Guard.NET&version=1.2.0

// Install Guard.NET as a Cake Tool
#tool nuget:?package=Guard.NET&version=1.2.0                


A simple library that facilitates runtime checks of code and allows 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.


public void AddUser(User user)
    Guard.NotNull(user, nameof(user), "optional custom error message");  // throws ArgumentNullException
    // OR use an explicit Exception
    var invalidOperationException = new InvalidOperationException("custom message");
    Guard.NotNull(user, invalidOperationException); 

public void GetUserByName(string name)
    Guard.NotNullOrWhitespace(name, nameof(name), "optional custom error message"); // throws ArgumentException

    // OR use an explicit Exception
    var invalidOperationException = new InvalidOperationException("custom message");
    Guard.NotNullOrWhitespace(userName, invalidOperationException);
public void GetUsers(int pageSize)
    Guard.NotGreaterThan(pageSize, _maxPageSize, nameof(pageSize), "optional custom error message"); // throws ArgumentOutOfRangeException

    // OR use an explicit Exception
    var invalidOperationException = new InvalidOperationException("custom message");
    Guard.NotGreaterThan(pageSize, _maxPageSize, invalidOperationException);
public void UpdateEmailAddress(int userId, string newEmailAddress)
    Guard.For(() => userId < 0, new ArgumentException(nameof(userId)));
    Guard.For(() => Regex.IsMatch(newEmailAddress, _emailRegexPattern), new ArgumentException(nameof(newEmailAddress)));



The Guard class can be used by installing Guard.NET nuget package available here.

Install-Package Guard.NET

The package has no external dependencies.

Product 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 was computed.  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. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 is compatible.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.3 is compatible.  netstandard1.4 was computed.  netstandard1.5 was computed.  netstandard1.6 was computed.  netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 2.1

    • No dependencies.
  • .NETFramework 4.5

    • No dependencies.
  • .NETFramework 4.6.1

    • No dependencies.
  • .NETStandard 1.3

  • .NETStandard 2.0

    • No dependencies.

NuGet packages (37)

Showing the top 5 NuGet packages that depend on Guard.NET:

Package Downloads

Provides capability to improve telemetry with Serilog in applications


Provides capability to use correlation in applications


Contains core functionality for Arcus.Security


Provides capabilities for easily integrating logging when building Web APIs running in Azure.


Provides core capabilities for easily integrating logging when building applications running in Azure.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Guard.NET:

Repository Stars
Bringing Azure Monitor metrics where you need them.
Version Downloads Last updated
3.0.0 350,956 8/26/2022
2.0.0 410,833 10/5/2021
1.2.0 732,518 8/24/2018
1.0.1 1,339 8/23/2018 5,075 6/25/2018 1,365 6/19/2018
1.0.0 2,252 4/10/2018

Added new Guards (NotIsMatch, NotEqualTom, NotAny, NotLessThanOrEqualTo, NotGreaterThanOrEqualTo).
Added new overloads for all Guards, allowing to specify a custom Exception as a generic parameter (without passing any concrete instance).