Guard.NET 1.0.1

.NET Core 2.1 .NET Standard 1.3 .NET Framework 4.5
There is a newer version of this package available.
See the version list below for details.
dotnet add package Guard.NET --version 1.0.1
NuGet\Install-Package Guard.NET -Version 1.0.1
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.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Guard.NET --version 1.0.1
#r "nuget: Guard.NET, 1.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. 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.0.1

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


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 Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp1.0 netcoreapp1.1 netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard1.3 netstandard1.4 netstandard1.5 netstandard1.6 netstandard2.0 netstandard2.1
.NET Framework net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen30 tizen40 tizen60
Universal Windows Platform uap uap10.0
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
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 (43)

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


Provides capability to improve telemetry with Serilog in applications


Contains core functionality for Arcus.Security


Provides support for Azure Key Vault

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 53,868 8/26/2022
2.0.0 151,504 10/5/2021
1.2.0 589,899 8/24/2018
1.0.1 678 8/23/2018 4,026 6/25/2018 822 6/19/2018
1.0.0 799 4/10/2018

Changed library root namespace to avoid name collision with the Guard class.
Added support for .Net Core 2.1 and .Net Standard 2.0