Light.GuardClauses 15.0.0

dotnet add package Light.GuardClauses --version 15.0.0
                    
NuGet\Install-Package Light.GuardClauses -Version 15.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="Light.GuardClauses" Version="15.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Light.GuardClauses" Version="15.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Light.GuardClauses" />
                    
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 Light.GuardClauses --version 15.0.0
                    
#r "nuget: Light.GuardClauses, 15.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 Light.GuardClauses@15.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=Light.GuardClauses&version=15.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Light.GuardClauses&version=15.0.0
                    
Install as a Cake Tool

Light.GuardClauses

A lightweight .NET library for expressive guard clauses.

License NuGet Source Code Documentation Changelog

Why Light.GuardClauses?

  • 🧰 130+ assertions cover nullability, collections, text, numbers, ranges, dates, URIs, types, streams, and more.
  • As fast as handwritten guards — most assertions are optimized and benchmarked against equivalent imperative checks using a dedicated BenchmarkDotNet suite.
  • 🏷️ Automatic parameter names - with C# 10 and newer, CallerArgumentExpression produces clear exceptions without repetitive nameof calls.
  • 🔄 Validation and assignment in one statement works because throwing guards return the successfully validated value.
  • 🧩 Custom exception factories let you control exception construction when the built-in exception does not fit your application.
  • 🧠 Tooling-aware contracts support Nullable Reference Types for Roslyn, .NET code analysis, and JetBrains annotations.
  • 🚀 Flexible deployment spans broad .NET compatibility, Native AOT, and optional single-file source inclusion.

Light.GuardClauses replaces repetitive parameter checks with expressive extension methods:

public class Foo
{
    private readonly IBar _bar;

    public Foo(IBar? bar) => _bar = bar.MustNotBeNull();
}

The guard returns the validated value, so validation and assignment can stay together. Throwing guards start with Must; Boolean checks such as IsNullOrEmpty, IsValidEnumValue, and IsFileExtension can be used in branching logic.

public void SetMovieRating(Guid movieId, int numberOfStars)
{
    movieId.MustNotBeEmpty();
    numberOfStars.MustBeIn(Range.InclusiveBetween(0, 5));

    var movie = _movieRepo.GetById(movieId);
    movie.AddRating(numberOfStars);
}

Custom exception factories let you replace a guard's default exception with one that fits your application. Some factories receive the values involved in the validation:

numberOfStars.MustBeIn(
    Range.InclusiveBetween(0, 5),
    (rating, range) => new InvalidOperationException(
        $"The rating {rating} is outside the allowed {range}."
    )
);

With C# 10 or newer, caller argument expressions automatically capture expressions such as movieId or numberOfStars for the exception parameter name. When using an older compiler, pass the name explicitly:

movieId.MustNotBeEmpty(nameof(movieId));

See the documentation index, assertion overview, and usage guide for more.

Target frameworks

The NuGet package contains three target-framework assets. NuGet automatically selects the best compatible asset for the consuming project:

  • .NET Standard 2.0 provides the portable Light.GuardClauses API with the broadest runtime compatibility. It is selected for .NET Framework and other implementations that support .NET Standard 2.0 but not 2.1.
  • .NET Standard 2.1 provides the portable Light.GuardClauses API for newer .NET implementations. It is selected for .NET Core 3.0+, .NET 5–9, and other implementations that support .NET Standard 2.1.
  • .NET 10 provides the full API for .NET 10 and later, including generic-math overloads, additional span and memory overloads, trimming annotations, framework-optimized implementations, and declared Native AOT compatibility.

Installation

Light.GuardClauses is available from NuGet.

  • .NET CLI: dotnet add package Light.GuardClauses
  • Package Manager Console: Install-Package Light.GuardClauses
  • Project file: <PackageReference Include="Light.GuardClauses" />

To embed the library without a DLL dependency, use the committed .NET Standard 2.0 single-file distribution or create a tailored file with the Light.GuardClauses.SourceCodeTransformation project.

Design and quality

The library supports nullable reference types, .NET code-analysis attributes, JetBrains contract annotations, and Native AOT. Its functional behavior is covered by the test suite, and performance-sensitive assertions can be measured with the current benchmark project.

For the design history behind guard clauses and design by contract, see Guard clause background.

Let there be Light!

Light Libraries logo

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.  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 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 is compatible. 
.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)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (28)

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

Package Downloads
Updatedge.net

C# wrapper for the updatedge API

Light.ViewModels

Simplify your XAML applications with Light.ViewModels!

Light.Xunit

Provides useful extensions for xunit.net test projects.

Synnotech.Migrations.Core

Core implementation of a generic migration engine that can be adapted to any target system.

Light.DependencyInjection

The DI Container you always wanted.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Light.GuardClauses:

Repository Stars
ExtendedXmlSerializer/home
A configurable and eXtensible Xml serializer for .NET.
Version Downloads Last Updated
15.0.0 59 7/15/2026
14.0.0 52,155 11/26/2025
13.1.0 40,918 7/21/2025
13.0.0 110,463 3/9/2025
12.0.0 368,041 10/27/2024
11.0.0 426,072 11/16/2023
11.0.0-preview1 250 9/24/2023
10.3.0 12,597 10/9/2023
10.2.0 7,783 9/22/2023
10.1.0 15,380 8/6/2023
10.0.0 561,069 12/6/2021
9.0.0 156,661 12/6/2020
8.1.0 63,314 5/27/2020
8.0.1 24,528 3/26/2020
8.0.0 1,726 3/25/2020
7.0.0 49,899 7/13/2019
6.2.0 18,933 2/12/2019
6.1.0 23,371 11/18/2018
6.0.0 1,978 11/10/2018
5.0.1 62,564 8/3/2018
Loading failed

Light.GuardClauses 15.0.0
     --------------------------------

     - new assertions: IsUuidVersion7, MustBeUuidVersion7, IsFinite, MustBeFinite, IsAscii, MustBeAscii, and MustHaveCountIn
     - new numeric sign guards: MustBePositive, MustBeNegative, MustNotBePositive, MustNotBeNegative, and MustNotBeZero
     - new dictionary key guards: MustContainKey and MustNotContainKey
     - new collection content guards: MustNotContainNull and MustNotContainNullOrWhiteSpace
     - new string inspection assertions: ContainsOnlyDigits, MustContainOnlyDigits, ContainsOnlyLettersOrDigits, MustContainOnlyLettersOrDigits, IsUpperCase, MustBeUpperCase, IsLowerCase, MustBeLowerCase, IsBase64, MustBeBase64, IsHexadecimal, MustBeHexadecimal, and MustNotContainWhiteSpace
     - new stream capability guards: MustBeReadable, MustBeWritable, and MustBeSeekable
     - new collection count guard: MustHaveSameCountAs
     - expanded span and memory support for MustNotBeEmpty, MustHaveLength, MustHaveLengthIn, and MustNotBeEmptyOrWhiteSpace, plus DateTimeOffset support for MustBeUtc
     - breaking: the modern package target was updated from .NET 8 to .NET 10; .NET Standard 2.0 and 2.1 remain supported