Skip to main content
Guard clauses for argument validation with CallerArgumentExpression for automatic parameter name capture.

The Problem

Standard null checks are verbose and require nameof:

The Solution

The parameter name is automatically captured - no nameof required.

Null Validation

NotNull

Validates non-null and returns the value:

NotNullOrElse

Returns value or default (no exception):

Member Validation

Validate an object and its member in one call:

String Validation

NotNullOrEmpty

Validates non-null and non-empty strings:

NotNullOrWhiteSpace

Validates strings contain meaningful content:

With Defaults

String Length Validation

HasLength

Validates exact string length:

HasMinLength / HasMaxLength

Validates minimum or maximum string length:

HasLengthBetween

Validates string length is within a range (inclusive):

Collection Validation

NotNullOrEmpty

NoDuplicates

Validates that a collection contains no duplicate elements:

Value Type Validation

NotDefault

Validates that a value type is not its default value:

NotEmpty (Guid)

Validates that a Guid is not empty:
NotEmpty for Guid is semantically clearer than NotDefault when working with identifiers, though they produce the same result for Guid.

Set Membership Validation

OneOf

Validates that a value is one of the allowed values:

NotOneOf

Validates that a value is not one of the disallowed values:

Range Validation

InRange

Validates value is within bounds (inclusive):

ValidIndex

Validates array/list index:

Numeric Validation

All numeric validation methods use [MethodImpl(AggressiveInlining)] for hot paths and are available for int, long, double, and decimal types.

Basic Constraints

Boundary Constraints

Double-Specific Validation

Double comparison methods (NotNegative, Positive, etc.) correctly handle NaN values by throwing ArgumentOutOfRangeException - NaN is not a valid number.

File System Validation

FileExists / DirectoryExists

Validates that a file or directory exists at the specified path:

Path Character Validation

Extension Validation

Type Validation

DefinedEnum

Validates that an enum value is defined:

Type Constraints

Condition Validation

That

Validates a boolean condition:

Satisfies

Validates value matches a predicate:

Unreachable Code

Mark code paths that should never execute:

UnreachableIf

Throws only when a condition is true, useful for asserting invariants:
Unreachable and UnreachableIf automatically capture caller info (CallerMemberName, CallerFilePath, CallerLineNumber) to provide detailed exception messages for debugging.

Examples

Constructor Validation

Method Parameter Validation

Configuration Validation

File Processing

API Reference

Null Validation

String Validation

Collection Validation

Value Type Validation

Set Membership Validation

File System Validation

Type Validation

Numeric Validation (int, long, double, decimal)

Double-Specific

Condition Validation

All validation methods use CallerArgumentExpressionAttribute to automatically capture the parameter name in exception messages - no nameof() required.