SGuard 2.1.0

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package SGuard --version 2.1.0
                    
NuGet\Install-Package SGuard -Version 2.1.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="SGuard" Version="2.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SGuard" Version="2.1.0" />
                    
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.1.0
                    
#r "nuget: SGuard, 2.1.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 SGuard@2.1.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=SGuard&version=2.1.0
                    
Install as a Cake Addin
#tool nuget:?package=SGuard&version=2.1.0
                    
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.1.0

  • License Change: Project license changed from GPL-3.0-or-later to MIT for .NET Foundation compatibility and broader commercial use.
  • All features from previous versions remain available.
  • See release notes for details.

📦 Installation

dotnet add package SGuard

📝 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);

🤝 Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

🌐 Code of Conduct

This project adheres to the .NET Foundation Code of Conduct. By participating, you are expected to uphold this code.

📜 License

This project is licensed under the MIT License, a permissive open source 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 (1)

Showing the top 1 NuGet packages that depend on SGuard:

Package Downloads
SGuard.DataAnnotations

Advanced, extensible, and multilingual data validation and guard clause library for .NET. Includes custom validation attributes, guard helpers, and resource-based error messages for enterprise-grade applications.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.1 87 9/5/2025

# Release Notes - Version 2.1.0

           ## 🔄 **License Change**
           - **BREAKING**: Changed license from GPL-3.0-or-later to **MIT License**
           - This change makes the library more permissive and suitable for commercial use
           - All previous functionality remains unchanged

           ## 📋 **Previous Features (Still Available)**
           - 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

           ## ⚠️ **Migration Notice**
           No code changes are required. This is purely a licensing change that provides more flexibility for users of the library.