SGuard 2.0.4

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

SGuard

SGuard is a lightweight, extensible guard clause library for .NET, providing expressive and robust validation for method arguments, object state, and business rules. It offers both boolean checks (Is.*) and exception-throwing guards (ThrowIf.*), with a unified callback model and rich exception diagnostics.

🚀 Features

  • Boolean Guards (Is.*): Check conditions without throwing exceptions.
  • Throwing Guards (ThrowIf.*): Throw exceptions when conditions are met.
  • Any & All Guards: Predicate-based validation for collections.
  • Comprehensive Comparison Guards: Between, LessThan, LessThanOrEqual, GreaterThan, GreaterThanOrEqual for generics and strings (with StringComparison).
  • Null/Empty Checks: Deep and type-safe null/empty validation for primitives, collections, and complex types.
  • Custom Exception Support: Overloads for custom exception types, with constructor argument support.
  • Callback Model: Unified SGuardCallback and GuardOutcome for success/failure handling.
  • Expression Caching: Efficient, thread-safe caching for compiled expressions.
  • Rich Exception Messages: Informative diagnostics using CallerArgumentExpression.
  • Multi-targeting: Supports .NET 6, 7, 8, and 9.

🆕 What's New in 2.0.4

  • Added LessThan and LessThanOrEqual guards for generics and strings.
  • Added Any<T> and All<T> guards with predicate support.
  • String guards now support StringComparison for culture-aware checks.
  • Unified callback support across all guards.
  • Custom exception overloads and constructor argument support.
  • Generic exception creation with ExceptionActivator.Create<T>, supporting both parameterless and parameterized constructors.
  • Expression caching for improved performance in null/empty checks.
  • Improved documentation and XML comments.

📦 Installation

dotnet add package SGuard --version 2.0.4

📝 Usage Examples

Boolean Guards

if (Is.Between(value, min, max)) { /* ... */ }
if (Is.LessThan(a, b)) { /* ... */ }
if (Is.Any(list, x => x > 0)) { /* ... */ }

Throwing Guards

ThrowIf.Between(value, min, max); // Throws if value is between min and max
ThrowIf.LessThan(a, b, () => Console.WriteLine("Failed!"));
ThrowIf.Any(list, x => x == null);

Custom Exceptions

ThrowIf.LessThanOrEqual(a, b, new MyCustomException("Invalid!"));
ThrowIf.Between<string, string, string, MyCustomException>(value, min, max, new MyCustomException("Out of range!"));

Null or Empty Checks

ThrowIf.NullOrEmpty(str);
ThrowIf.NullOrEmpty(obj, x => x.Property);

📄 License

This project is licensed under the GPL-3.0-or-later license. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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.  net8.0 is compatible.  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 is compatible.  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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.4 58 8/29/2025
2.0.3 69 8/27/2025
2.0.2 87 8/26/2025
2.0.1 268 8/25/2025
2.0.0 63 8/22/2025
1.2.2 366 12/15/2023 1.2.2 is deprecated because it is no longer maintained and has critical bugs.

- Version: 2.0.4
         Highlights
         - Any<T> and All<T> guards with predicate support
         - Support for IComparable<T> types with generic comparison
         - String comparison with StringComparison parameter for culture-specific comparisons
         - Culture-aware string comparisons for Between, LessThan, and LessThanOrEqual operations
         - Consistent callback support across all comparison methods
         - Multiple overloads for custom exception types
         - Constructor argument support for exception instantiation
         - Generic exception creation with ExceptionActivator.Create<T>
         - Support for both parameterless and parameterized exception constructors
         - Expression Caching: Implemented ConcurrentDictionary for compiled expression caching
         - Reduced compilation overhead for repeated null/empty checks with selectors
         - Thread-safe expression evaluation