CCross.ThrowIf is a lightweight C# library that provides expressive, fluent guard clauses for argument validation and exception throwing. Stop writing boilerplate if (...) throw new ... blocks — express your preconditions cleanly in one line.
- Fluent guard clauses —
ThrowIf.Argument.IsNull,IsNullOrEmpty,IsGreaterThan, and more - Strongly-typed expressions — uses
Expression<Func<T>>to automatically capture the argument name (no morenameof(...)) - Multiple type support — works with
string,int,long,float,double,decimal,bool,DateTime,TimeSpan - Generic overloads —
ThrowIf<TException>lets you specify the exception type to throw - Clean API — organized into logical categories:
Argument,Collection,Value,ArrayIndex - Zero dependencies — pure .NET Portable Class Library
dotnet add package CCrossThrowIfOr via the NuGet Package Manager:
Install-Package CCrossThrowIf
Note: Targets .NET Portable (Profile 7 / .NET 4.5+). Compatible with .NET Standard projects.
using CCrossThrowIf;
public class OrderService
{
public void PlaceOrder(string customerId, int quantity, decimal amount)
{
// Throws ArgumentNullException with auto-captured argument name
ThrowIf.Argument.IsNullOrWhiteSpace(() => customerId);
// Throws ArgumentOutOfRangeException if quantity <= 0
ThrowIf.Argument.IsNegativeOrZero(() => quantity);
// Throws ArgumentOutOfRangeException if amount > 10000
ThrowIf.Argument.IsGreaterThan(() => amount, limit: 10000m, "Order amount exceeds maximum.");
// Process the order...
}
}// Throw any exception type with the same fluent API
ThrowIf<InvalidOperationException>.IsNull(() => myService);
ThrowIf<ArgumentOutOfRangeException>.IsNullOrEmpty(() => configValue, "Config must be set.");ThrowIf.Argument.IsInThePast(() => scheduledDate);
ThrowIf.Argument.IsInTheFuture(() => auditTimestamp);ThrowIf.Argument.IsTrue(() => isLocked, "Resource is locked and cannot be modified.");
ThrowIf.Argument.IsFalse(() => isInitialized);Full API reference and examples available at the project website.
Pull requests welcome. See CONTRIBUTING.md if available, or open an issue to discuss your changes.
MIT — see LICENSE