Wiaoj.Preconditions
0.0.1-alpha.52
dotnet add package Wiaoj.Preconditions --version 0.0.1-alpha.52
NuGet\Install-Package Wiaoj.Preconditions -Version 0.0.1-alpha.52
<PackageReference Include="Wiaoj.Preconditions" Version="0.0.1-alpha.52" />
<PackageVersion Include="Wiaoj.Preconditions" Version="0.0.1-alpha.52" />
<PackageReference Include="Wiaoj.Preconditions" />
paket add Wiaoj.Preconditions --version 0.0.1-alpha.52
#r "nuget: Wiaoj.Preconditions, 0.0.1-alpha.52"
#:package Wiaoj.Preconditions@0.0.1-alpha.52
#addin nuget:?package=Wiaoj.Preconditions&version=0.0.1-alpha.52&prerelease
#tool nuget:?package=Wiaoj.Preconditions&version=0.0.1-alpha.52&prerelease
Wiaoj.Preconditions (Preca)
A high-performance, modern, and zero-allocation focused precondition and guard clause library for .NET.
🚀 Why Preca?
- Extreme Performance: Built with
[MethodImpl(MethodImplOptions.AggressiveInlining)]and theThrowerpattern to keep the "hot path" clean for the CPU cache. - Clean Stack Traces: Uses
[StackTraceHidden]so that exceptions point directly to the caller, not the library internals. - Modern .NET Core: Native support for C# 12+ and .NET 8+ features like Generic Math (
ISignedNumber,IComparisonOperators) and Buffers (ReadOnlySpan<T>,Memory<T>). - Zero Allocation: Designed to avoid unnecessary boxing or allocations during successful validation.
- Fluent & Static: Offers both a standard static API (
Preca.ThrowIfNull) and a fluent gateway via extensions.
📦 Installation
Install via NuGet:
dotnet add package Wiaoj.Preconditions
🛠 Usage
Basic Validations
Standard null and string checks with automatic parameter name capturing:
public void UpdateUser(string id, string displayName)
{
// Throws PrecaArgumentNullException if null
Preca.ThrowIfNull(id);
// Throws PrecaArgumentException if null, empty, or whitespace
Preca.ThrowIfNullOrWhiteSpace(displayName);
}
Numeric & Range Validations (Generic Math)
Preca leverages .NET Generic Math to provide type-safe validations for any numeric type (int, decimal, double, BigInteger, etc.):
public void ProcessOrder(int quantity, decimal price)
{
Preca.ThrowIfNegativeOrZero(quantity);
Preca.ThrowIfLessThan(price, 0.99m);
// Inclusive range check
Preca.ThrowIfOutOfRange(quantity, 1, 1000);
}
Fluent API Extensions
Use the Extensions gateway for a more fluid syntax or when you want to return the validated value:
public class UserService
{
private readonly string _apiKey;
public UserService(string apiKey)
{
// Validates and returns the value in one line
_apiKey = Preca.Extensions.ThrowIfNullOrWhiteSpace(apiKey);
}
}
Custom Exception Factories
When you need to throw domain-specific exceptions instead of standard ArgumentExceptions:
Preca.ThrowIf(balance < total, () => new InsufficientFundsException("Account balance too low."));
// Or with state to avoid closure allocations
Preca.ThrowIfNull(user, (u) => new UserNotFoundException(u.Id), user);
Buffer & Span Support
Optimized checks for memory-efficient types:
public void ParseData(ReadOnlySpan<char> buffer)
{
Preca.ThrowIfEmpty(buffer);
Preca.ThrowIfEmptyOrWhiteSpace(buffer);
}
📋 Supported Validations
| Category | Methods |
|---|---|
| Nullability | ThrowIfNull |
| Strings | ThrowIfNullOrEmpty, ThrowIfNullOrWhiteSpace |
| Numerics | ThrowIfNegative, ThrowIfPositive, ThrowIfZero, ThrowIfLessThan, ThrowIfGreaterThan, ThrowIfOutOfRange, ThrowIfMaxValue, ThrowIfMinValue |
| Floating Point | ThrowIfNaN, ThrowIfInfinity, ThrowIfSubnormal |
| Buffers | ThrowIfEmpty (Span, ReadOnlySpan, Memory, ArraySegment) |
| Value Types | ThrowIfDefault, ThrowIfEmpty (Guid), ThrowIfUnspecifiedKind (DateTime), ThrowIfUndefined (Enum) |
| Booleans | ThrowIf, ThrowIfTrue, ThrowIfFalse |
🏗 Performance Design
Preca uses the Thrower Pattern. Static methods that throw exceptions are often not inlined by the JIT compiler because the code size is too large.
Preca moves the throw statement to a specialized Thrower class. This allows the guard clause itself to be inlined into your method, effectively reducing the cost of a successful check to a single CPU branch instruction.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
-
net10.0
- No dependencies.
NuGet packages (14)
Showing the top 5 NuGet packages that depend on Wiaoj.Preconditions:
| Package | Downloads |
|---|---|
|
Wiaoj.Primitives
A set of high-performance, zero-allocation, and secure primitive types (Secret, Base64String, SemVer, Percentage) to combat primitive obsession in .NET. |
|
|
Wiaoj.Abstractions
A foundational library providing standardized, type-safe interfaces for common architectural patterns in .NET. Includes generic support for Deep/Shallow Cloning, State Copying (CopyFrom/CopyTo), and Asynchronous Object Factories to promote loose coupling and clear contracts. |
|
|
Wiaoj.Serialization.DependencyInjection
ASP.NET Core dependency injection integration for Wiaoj Serialization. |
|
|
Wiaoj.Extensions.DependencyInjection
Advanced Dependency Injection extensions for .NET, featuring a robust Decorator pattern implementation using modern C# syntax. Supports all service lifetimes and registration types. |
|
|
Wiaoj.Serialization.SystemTextJson
High-performance System.Text.Json implementation for Wiaoj Serialization with configurable options and telemetry support. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.0.1-alpha.52 | 0 | 4/16/2026 |
| 0.0.1-alpha.51 | 0 | 4/16/2026 |
| 0.0.1-alpha.50 | 68 | 4/15/2026 |
| 0.0.1-alpha.49 | 52 | 4/15/2026 |
| 0.0.1-alpha.48 | 58 | 4/10/2026 |
| 0.0.1-alpha.47 | 59 | 4/9/2026 |
| 0.0.1-alpha.46 | 48 | 4/9/2026 |
| 0.0.1-alpha.45 | 56 | 4/8/2026 |
| 0.0.1-alpha.44 | 55 | 4/8/2026 |
| 0.0.1-alpha.43 | 567 | 4/3/2026 |
| 0.0.1-alpha.42 | 67 | 4/2/2026 |
| 0.0.1-alpha.41 | 62 | 4/2/2026 |
| 0.0.1-alpha.40 | 69 | 4/2/2026 |
| 0.0.1-alpha.39 | 62 | 3/31/2026 |
| 0.0.1-alpha.38 | 74 | 3/22/2026 |
| 0.0.1-alpha.37 | 56 | 3/22/2026 |
| 0.0.1-alpha.36 | 56 | 3/22/2026 |
| 0.0.1-alpha.35 | 60 | 3/22/2026 |
| 0.0.1-alpha.34 | 54 | 3/21/2026 |
| 0.0.1-alpha.33 | 61 | 3/21/2026 |