Ardalis.GuardClauses 4.1.1

.NET 7.0 .NET Standard 2.0
dotnet add package Ardalis.GuardClauses --version 4.1.1
NuGet\Install-Package Ardalis.GuardClauses -Version 4.1.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="Ardalis.GuardClauses" Version="4.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Ardalis.GuardClauses --version 4.1.1
#r "nuget: Ardalis.GuardClauses, 4.1.1"
#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 Ardalis.GuardClauses as a Cake Addin
#addin nuget:?package=Ardalis.GuardClauses&version=4.1.1

// Install Ardalis.GuardClauses as a Cake Tool
#tool nuget:?package=Ardalis.GuardClauses&version=4.1.1

<h1 align=center> <img src="media/logotype 1024.svg" width=50%> </h1>

NuGetNuGet publish Ardalis.GuardClauses to nuget

<a href=""> <img src="" alt="Follow @ardalis" /> </a>   <a href=""> <img src="" alt="Follow @nimblepros" /> </a>

Guard Clauses

A simple extensible package with guard clause extensions.

A guard clause is a software pattern that simplifies complex functions by "failing fast", checking for invalid inputs up front and immediately failing if any are found.

Give a Star! ⭐

If you like or are using this project please give it a star. Thanks!


public void ProcessOrder(Order order)

    // process order here

// OR

public class Order
    private string _name;
    private int _quantity;
    private long _max;
    private decimal _unitPrice;
    private DateTime _dateCreated;

    public Order(string name, int quantity, long max, decimal unitPrice, DateTime dateCreated)
        _name = Guard.Against.NullOrWhiteSpace(name);
        _quantity = Guard.Against.NegativeOrZero(quantity);
        _max = Guard.Against.Zero(max);
        _unitPrice = Guard.Against.Negative(unitPrice);
        _dateCreated = Guard.Against.OutOfSQLDateRange(dateCreated, dateCreated);

Supported Guard Clauses

  • Guard.Against.Null (throws if input is null)
  • Guard.Against.NullOrEmpty (throws if string, guid or array input is null or empty)
  • Guard.Against.NullOrWhiteSpace (throws if string input is null, empty or whitespace)
  • Guard.Against.OutOfRange (throws if integer/DateTime/enum input is outside a provided range)
  • Guard.Against.EnumOutOfRange (throws if an enum value is outside a provided Enum range)
  • Guard.Against.OutOfSQLDateRange (throws if DateTime input is outside the valid range of SQL Server DateTime values)
  • Guard.Against.Zero (throws if number input is zero)
  • Guard.Against.Expression (use any expression you define)
  • Guard.Against.InvalidFormat (define allowed format with a regular expression or func)
  • Guard.Against.NotFound (similar to Null but for use with an id/key lookup; throws a NotFoundException)

Extending with your own Guard Clauses

To extend your own guards, you can do the following:

// Using the same namespace will make sure your code picks up your 
// extensions no matter where they are in your codebase.
namespace Ardalis.GuardClauses
    public static class FooGuard
        public static void Foo(this IGuardClause guardClause,
            string input, 
            [CallerArgumentExpression("input")] string? parameterName = null)
            if (input?.ToLower() == "foo")
                throw new ArgumentException("Should not have been foo!", parameterName);

// Usage
public void SomeMethod(string something)
    Guard.Against.Foo(something, nameof(something)); // optional - provide parameter name

Breaking Changes in v4

  • OutOfRange for Enums now uses EnumOutOfRange
  • Custom error messages now work more consistently, which may break some unit tests

Nice Visualization of Refactoring to use Guard Clauses

via Nicolas Carlo


Commercial Support

If you require commercial support to include this library in your applications, contact NimblePros

Build Notes (for maintainers)

  • Remember to update the PackageVersion in the csproj file and then a build on main should automatically publish the new package to
  • Add a release with form 1.3.2 to GitHub Releases in order for the package to actually be published to Nuget. Otherwise it will claim to have been successful but is lying to you.
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 is compatible.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  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 tizen40 was computed.  tizen60 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)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net7.0

    • No dependencies.

NuGet packages (153)

Showing the top 5 NuGet packages that depend on Ardalis.GuardClauses:

Package Downloads

Nuget package for common helpers and enums


A collection of functionalities common to many applications, gathered to support the rapid development of a wide variety of applications targeting multiple platforms.


Package Description


A C# Client for the MockServer Docker Image


MONAI Deploy communication system between clinical data pipelines components.

GitHub repositories (14)

Showing the top 5 popular GitHub repositories that depend on Ardalis.GuardClauses:

Repository Stars
Clean Architecture Solution Template: A starting point for Clean Architecture with ASP.NET Core
Clean Architecture Solution Template for ASP.NET Core
Sample ASP.NET Core 7.0 reference application, powered by Microsoft, demonstrating a layered application architecture with monolithic deployment model. Download the eBook PDF from docs folder.
smtp4dev - the fake smtp email server for development and testing
A solution template using Clean Architecture for building a .NET Core Worker Service.
Version Downloads Last updated
4.1.1 144,436 8/1/2023
4.1.0 282 8/1/2023
4.0.1 4,716,400 3/28/2022
4.0.0 670,117 1/18/2022
3.3.0 961,098 10/29/2021
3.2.0 1,344,300 5/14/2021
3.1.0 522,824 2/13/2021
3.0.1 608,287 10/9/2020
3.0.0 191,834 8/20/2020
2.0.0 93,837 7/22/2020
1.5.0 802,199 4/29/2020
1.4.2 106,410 3/16/2020
1.4.1 85,557 2/13/2020
1.4.0 2,796 2/13/2020
1.3.3 60,649 1/18/2020
1.3.2 9,241 1/3/2020
1.3.1 65,026 11/15/2019
1.2.9 207,491 7/11/2019
1.2.8 90,270 2/11/2019
1.2.7 9,147 2/4/2019
1.2.6 2,422 1/24/2019
1.2.5 5,909 1/23/2019
1.2.4 425,063 11/26/2018
1.2.3 179,320 12/4/2017
1.2.2 2,226 12/1/2017
1.2.1 1,794 11/13/2017
1.2.0 1,438 11/13/2017
1.1.1 16,538 9/27/2017
1.1.0 1,099 9/26/2017
1.0.3 1,374 9/22/2017
1.0.2 1,444 9/22/2017
1.0.1 1,462 9/22/2017
1.0.0 14,556 9/12/2017

* Implemented NullOrInvalidInput method by @tiagojsrios in
           * Embed license in nuget package  by @jafin in
           * Remove JetBrains Annotations by @Coop56 in
           * Add NullOrEmpty and NullOrWhiteSpace overloads for ReadOnlySpan by @KonH in
           * Created async overload for clausules that use a predicate by @danny-bos-developer in
           * Working through build issues for new contributors by @sadukie in
           * Make type parameter T of the Null() method nullable and add overload for value types. by @amal-stack in
           * Upgrade to dotnet 7.0 by @amal-stack in
           * Adds `NullOrOutOfRange` clauses, adding support for nullable classes/structs to `OutOfRange` by @rafaelsc in
           * Add README file to package