SGuard 2.0.4
dotnet add package SGuard --version 2.0.4
NuGet\Install-Package SGuard -Version 2.0.4
<PackageReference Include="SGuard" Version="2.0.4" />
<PackageVersion Include="SGuard" Version="2.0.4" />
<PackageReference Include="SGuard" />
paket add SGuard --version 2.0.4
#r "nuget: SGuard, 2.0.4"
#:package SGuard@2.0.4
#addin nuget:?package=SGuard&version=2.0.4
#tool nuget:?package=SGuard&version=2.0.4
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 (withStringComparison
). - 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
andGuardOutcome
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
andLessThanOrEqual
guards for generics and strings. - Added
Any<T>
andAll<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.
🔗 Links
Product | Versions 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. |
-
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: 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