Table of Contents

ThrowHelper Class

Definition

Namespace
Bodu
Assembly
Bodu.Core.dll
Package
Bodu.Core 1.0.1
Source
ThrowHelper.Array-CallerExpression.cs

Provides centralized guard clause methods for argument validation, offering consistent and concise exception throwing across the library.

public static class ThrowHelper
Inheritance
ThrowHelper
Inherited Members

Remarks

This class is split into per-group partial files following the pattern ThrowHelper.<Group>-CallerExpression.cs (for example ThrowHelper.Array-CallerExpression.cs). Each guard relies on CallerArgumentExpressionAttribute so the compiler automatically captures argument expressions at the call site.

Methods

ThrowIfAddOverflows(int, int, string?)

Throws an ArgumentOutOfRangeException if the sum of left and right would fall outside the int range.

public static void ThrowIfAddOverflows(int left, int right, string? paramName = null)

Parameters

left int

The left addend.

right int

The right addend.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Remarks

Guards input-controlled size or offset arithmetic against silent wrap-around before the result is used to index, slice, or allocate. The check is performed in a wider type so no overflow occurs during validation.

Exceptions

ArgumentOutOfRangeException

Thrown when left + right would overflow or underflow the int range.

ThrowIfAddOverflows(long, long, string?)

Throws an ArgumentOutOfRangeException if the sum of left and right would fall outside the long range.

public static void ThrowIfAddOverflows(long left, long right, string? paramName = null)

Parameters

left long

The left addend.

right long

The right addend.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Remarks

Guards input-controlled size or offset arithmetic against silent wrap-around before the result is used to index, slice, or allocate. The check is performed in a wider type so no overflow occurs during validation.

Exceptions

ArgumentOutOfRangeException

Thrown when left + right would overflow or underflow the long range.

ThrowIfArrayContainsNonNumeric(Array?, string?)

Throws an ArgumentNullException if the array is null, or an ArgumentException if it contains any non-numeric element.

public static void ThrowIfArrayContainsNonNumeric(Array? array, string? paramName = null)

Parameters

array Array

The array to validate. Must not be null.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Remarks

Validates that each non-null element is one of the primitive numeric types: byte, sbyte, short, ushort, int, uint, long, ulong, float, double, or decimal. Nullable wrappers are unwrapped before the type check is applied.

Exceptions

ArgumentNullException

Thrown when array is null.

ArgumentException

Thrown when any element in array is not a recognized numeric type.

ThrowIfArrayIsNotZeroBased(Array?, string?)

Throws an ArgumentNullException if the array is null, or an ArgumentException if it does not have a zero lower bound.

public static void ThrowIfArrayIsNotZeroBased(Array? array, string? paramName = null)

Parameters

array Array

The array to validate. Must not be null.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Exceptions

ArgumentNullException

Thrown when array is null.

ArgumentException

Thrown when array.GetLowerBound(0) != 0.

ThrowIfArrayLengthIsInsufficient(Array?, int, int, string?, string?)

Throws an ArgumentNullException if the array is null, an ArgumentOutOfRangeException if index is negative, or an ArgumentException if array does not have enough room from index to accommodate requiredLength elements.

public static void ThrowIfArrayLengthIsInsufficient(Array? array, int index, int requiredLength, string? paramName = null, string? indexParamName = null)

Parameters

array Array

The array to validate. Must not be null.

index int

The zero-based starting index within the array.

requiredLength int

The number of elements required starting from index.

paramName string

The name of the array parameter. Supplied automatically by the compiler via CallerArgumentExpressionAttribute.

indexParamName string

The name of the index parameter. Supplied automatically by the compiler via CallerArgumentExpressionAttribute.

Remarks

The available room is computed by subtraction (array.Length - index) rather than by adding index and requiredLength, so the check cannot be defeated by an integer overflow when either operand is large.

Exceptions

ArgumentNullException

Thrown when array is null.

ArgumentOutOfRangeException

Thrown when index is negative.

ArgumentException

Thrown when array.Length - index is less than requiredLength.

ThrowIfArrayLengthIsInsufficient(Array?, int, string?)

Throws an exception if the specified array has fewer than minimumLength elements.

public static void ThrowIfArrayLengthIsInsufficient(Array? array, int minimumLength, string? paramName = null)

Parameters

array Array

The array to validate. Must not be null.

minimumLength int

The minimum number of elements that array must contain.

paramName string

The name of the array parameter. Supplied automatically by the compiler via CallerArgumentExpressionAttribute.

Remarks

Use this overload when the caller may supply a larger array than required and the excess elements are simply ignored - for example, a buffer that must hold at least a full cipher block but may be larger. When the length must be exact, use ThrowIfArrayLengthIsNotEqualTo(Array?, int, string?) instead.

Exceptions

ArgumentNullException

Thrown when array is null.

ArgumentException

Thrown when array.Length is less than minimumLength.

ThrowIfArrayLengthIsNotEqualTo(Array?, int, string?)

Throws an exception if the specified array does not have exactly expectedLength elements.

public static void ThrowIfArrayLengthIsNotEqualTo(Array? array, int expectedLength, string? paramName = null)

Parameters

array Array

The array to validate. Must not be null.

expectedLength int

The exact number of elements that array must contain.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Remarks

Commonly used in cryptographic APIs or buffer transformations where a fixed-size input is mandatory (e.g. 16 bytes for a cipher block, 32 bytes for a key).

Exceptions

ArgumentNullException

Thrown when array is null.

ArgumentException

Thrown when array.Length does not equal expectedLength.

ThrowIfArrayLengthIsZero(Array, string?)

Throws an ArgumentNullException if the array is null, or an ArgumentException if it has zero length.

public static void ThrowIfArrayLengthIsZero(Array array, string? paramName = null)

Parameters

array Array

The array to validate. Must not be null.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Exceptions

ArgumentNullException

Thrown when array is null.

ArgumentException

Thrown when array.Length == 0.

ThrowIfArrayLengthNotPositiveMultipleOf(Array, int, string?)

Throws an ArgumentNullException if the array is null, or an ArgumentException if its length is not a positive multiple of divisor.

public static void ThrowIfArrayLengthNotPositiveMultipleOf(Array array, int divisor, string? paramName = null)

Parameters

array Array

The array to validate. Must not be null.

divisor int

The required positive divisor.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Exceptions

ArgumentNullException

Thrown when array is null.

ArgumentException

Thrown when array.Length == 0 or array.Length % divisor != 0.

ThrowIfArrayLengthOutOfRange(Array, int, int, string?)

Throws an ArgumentNullException if the array is null, or an ArgumentOutOfRangeException if its length is not between minLength and maxLength (inclusive).

public static void ThrowIfArrayLengthOutOfRange(Array array, int minLength, int maxLength, string? paramName = null)

Parameters

array Array

The array to validate. Must not be null.

minLength int

The minimum permitted length, inclusive. Must be zero or greater.

maxLength int

The maximum permitted length, inclusive. Must be greater than or equal to minLength.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Exceptions

ArgumentNullException

Thrown when array is null.

ArgumentOutOfRangeException

Thrown when array.Length < minLength or array.Length > maxLength.

ThrowIfArrayMultidimensional(Array?, string?)

Throws an ArgumentNullException if the array is null, or an ArgumentException if it is not single-dimensional.

public static void ThrowIfArrayMultidimensional(Array? array, string? paramName = null)

Parameters

array Array

The array to validate. Must not be null.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Exceptions

ArgumentNullException

Thrown when array is null.

ArgumentException

Thrown when array has a rank other than 1.

ThrowIfArrayOffsetOrCountInvalid(Array, int, int, string?, string?, string?)

Throws an ArgumentNullException if the array is null, an ArgumentOutOfRangeException if offset or count is out of range, or an ArgumentException if the segment they define exceeds the bounds of array .

public static void ThrowIfArrayOffsetOrCountInvalid(Array array, int offset, int count, string? paramArrayName = null, string? paramOffsetName = null, string? paramCountName = null)

Parameters

array Array

The array to validate. Must not be null.

offset int

The zero-based starting index within the array.

count int

The number of elements to access from offset.

paramArrayName string

The name of the array parameter. Supplied automatically by the compiler.

paramOffsetName string

The name of the index parameter. Supplied automatically by the compiler.

paramCountName string

The name of the count parameter. Supplied automatically by the compiler.

Exceptions

ArgumentNullException

Thrown when array is null.

ArgumentOutOfRangeException

Thrown when offset or count is negative or exceeds array.Length.

ArgumentException

Thrown when index + count exceeds array.Length.

ThrowIfArrayTypeIsNotCompatible<TExpected>(Array, string?)

Throws an ArgumentNullException if the array is null, or an ArgumentException if it is not assignable to TExpected[].

public static void ThrowIfArrayTypeIsNotCompatible<TExpected>(Array array, string? paramName = null)

Parameters

array Array

The array to validate. Must not be null.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

TExpected

The expected element type.

Remarks

The null guard is applied before the pattern match because a null reference satisfies is not TExpected[], which would otherwise produce an ArgumentException rather than an ArgumentNullException .

Exceptions

ArgumentNullException

Thrown when array is null.

ArgumentException

Thrown when array is not of type TExpected[].

ThrowIfCollectionIsEmpty<T>(ICollection<T>, string?)

Throws an ArgumentException if the collection is empty.

public static void ThrowIfCollectionIsEmpty<T>(ICollection<T> collection, string? paramName = null)

Parameters

collection ICollection<T>

The collection to validate. Must not be null.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

The element type of the collection.

Exceptions

ArgumentNullException

Thrown when collection is null.

ArgumentException

Thrown when collection.Count == 0.

ThrowIfCollectionTooSmall<T>(ICollection<T>, int, string?)

Throws an ArgumentException if the collection has fewer than minCount elements.

public static void ThrowIfCollectionTooSmall<T>(ICollection<T> collection, int minCount, string? paramName = null)

Parameters

collection ICollection<T>

The collection to validate. Must not be null.

minCount int

The minimum number of required elements.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

The element type of the collection.

Exceptions

ArgumentNullException

Thrown when collection is null.

ArgumentException

Thrown when collection.Count < minCount.

ThrowIfConditionallyRequiredParameterIsNull<TValue, TCondition>(TValue?, TCondition, TCondition, string?, string?)

Throws an ArgumentException if value is null when conditionalParam equals conditionalValue.

public static void ThrowIfConditionallyRequiredParameterIsNull<TValue, TCondition>(TValue? value, TCondition conditionalParam, TCondition conditionalValue, string? paramName = null, string? conditionalParamName = null)

Parameters

value TValue

The parameter value to validate for null.

conditionalParam TCondition

The current value of the conditional parameter.

conditionalValue TCondition

The value of conditionalParam that makes value mandatory.

paramName string

The name of the value parameter. Supplied automatically by the compiler.

conditionalParamName string

The name of the conditional parameter. Supplied automatically by the compiler.

Type Parameters

TValue

The type of the parameter being validated.

TCondition

The type of the conditional parameter.

Remarks

Use this method when a parameter becomes mandatory depending on the value of another parameter.

Exceptions

ArgumentException

Thrown when conditionalParam equals conditionalValue and value is null.

ThrowIfCountExceedsAvailable(int, int, string?)

Throws an ArgumentOutOfRangeException if count is negative or exceeds available.

public static void ThrowIfCountExceedsAvailable(int count, int available, string? paramName = null)

Parameters

count int

The count value to validate.

available int

The number of available items.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Remarks

Use this method when validating that a subset operation will not exceed the size of the source.

Exceptions

ArgumentOutOfRangeException

Thrown when count < 0 or count > available.

ThrowIfDestinationSpanTooSmall<TSource, TDestination>(ReadOnlySpan<TSource>, Span<TDestination>, string?)

Throws an ArgumentException if destination is shorter than source.

public static void ThrowIfDestinationSpanTooSmall<TSource, TDestination>(ReadOnlySpan<TSource> source, Span<TDestination> destination, string? paramDestinationName = null)

Parameters

source ReadOnlySpan<TSource>

The source span.

destination Span<TDestination>

The destination span.

paramDestinationName string

The name of the destination parameter. Supplied automatically by the compiler.

Type Parameters

TSource

The element type of the source span.

TDestination

The element type of the destination span.

Remarks

Useful for validating buffer-to-buffer operations such as copying or endian-swapping.

Exceptions

ArgumentException

Thrown when destination.Length < source.Length.

ThrowIfDestinationTooSmall<TSource, TDestination>(TSource[], TDestination[], string?)

Throws an ArgumentNullException if either array is null, or an ArgumentException if destination is shorter than source.

public static void ThrowIfDestinationTooSmall<TSource, TDestination>(TSource[] source, TDestination[] destination, string? paramDestinationName = null)

Parameters

source TSource[]

The source array. Must not be null.

destination TDestination[]

The destination array. Must not be null.

paramDestinationName string

The name of the destination parameter. Supplied automatically by the compiler.

Type Parameters

TSource

The element type of the source array.

TDestination

The element type of the destination array.

Remarks

Useful for validating array-to-array operations such as copying or endian-swapping.

Exceptions

ArgumentNullException

Thrown when source or destination is null.

ArgumentException

Thrown when destination.Length < source.Length.

ThrowIfDisposed(bool, string?)

Throws an ObjectDisposedException if disposed is true.

public static void ThrowIfDisposed(bool disposed, string? objectName = null)

Parameters

disposed bool

The disposal flag to evaluate.

objectName string

The name of the disposed object included in the exception message. Callers should pass this explicitly - typically nameof(TypeName) or GetType().FullName.

Remarks

When objectName is omitted, CallerArgumentExpressionAttribute captures the text of the disposed argument expression (for example "_disposed"), not the owning type's name - producing a misleading ObjectName. Always supply objectName explicitly at call sites.

Exceptions

ObjectDisposedException

Thrown when disposed is true.

ThrowIfEnumValueIsUndefined<TEnum>(TEnum, string?)

Throws an ArgumentOutOfRangeException if value is not a defined member of TEnum.

public static void ThrowIfEnumValueIsUndefined<TEnum>(TEnum value, string? paramName = null) where TEnum : struct, Enum

Parameters

value TEnum

The value to validate.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

TEnum

The enum type.

Exceptions

ArgumentOutOfRangeException

Thrown when value is not a defined member of TEnum.

ThrowIfEqual<T>(T, T, string?)

Throws an ArgumentOutOfRangeException if value equals other.

public static void ThrowIfEqual<T>(T value, T other, string? paramName = null) where T : IEquatable<T>

Parameters

value T

The value to validate.

other T

The value that value must not equal.

paramName string

The name of the value parameter. Supplied automatically by the compiler.

Type Parameters

T

A type that implements IEquatable<T>.

Exceptions

ArgumentOutOfRangeException

Thrown when value equals other.

ThrowIfGreaterThanOrEqualOther<T>(T, T, string?, string?)

Throws an ArgumentException if value is greater than or equal to other.

public static void ThrowIfGreaterThanOrEqualOther<T>(T value, T other, string? paramName = null, string? otherName = null) where T : IComparable<T>

Parameters

value T

The value being validated.

other T

The comparison reference value.

paramName string

The name of the value parameter. Supplied automatically by the compiler.

otherName string

The name of the comparison parameter. Supplied automatically by the compiler.

Type Parameters

T

A comparable type.

Exceptions

ArgumentException

Thrown when value >= other.

ThrowIfGreaterThanOrEqual<T>(T, T, string?)

Throws an ArgumentOutOfRangeException if value is greater than or equal to max.

public static void ThrowIfGreaterThanOrEqual<T>(T value, T max, string? paramName = null) where T : IComparable<T>

Parameters

value T

The value to compare.

max T

The exclusive upper bound.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

A comparable type.

Exceptions

ArgumentOutOfRangeException

Thrown when value >= max.

ThrowIfGreaterThanOther<T>(T, T, string?, string?)

Throws an ArgumentException if value is greater than other.

public static void ThrowIfGreaterThanOther<T>(T value, T other, string? paramName = null, string? otherName = null) where T : IComparable<T>

Parameters

value T

The value being validated.

other T

The comparison reference value.

paramName string

The name of the value parameter. Supplied automatically by the compiler.

otherName string

The name of the comparison parameter. Supplied automatically by the compiler.

Type Parameters

T

A comparable type.

Exceptions

ArgumentException

Thrown when value > other.

ThrowIfGreaterThan<T>(T, T, string?)

Throws an ArgumentOutOfRangeException if value is greater than max.

public static void ThrowIfGreaterThan<T>(T value, T max, string? paramName = null) where T : IComparable<T>

Parameters

value T

The value to compare.

max T

The inclusive upper bound.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

A comparable type.

Exceptions

ArgumentOutOfRangeException

Thrown when value > max.

ThrowIfIndexOutOfRange(long, Array, string?)

Throws an ArgumentOutOfRangeException if index is outside the valid range of array.

public static void ThrowIfIndexOutOfRange(long index, Array array, string? paramName = null)

Parameters

index long

The index to validate.

array Array

The array against which to validate the index.

paramName string

The name of the index parameter. Supplied automatically by the compiler.

Exceptions

ArgumentNullException

Thrown when array is null.

ArgumentOutOfRangeException

Thrown when index < 0 or >= array.LongLength.

ThrowIfIndexOutOfRange<T>(int, ReadOnlySpan<T>, string?)

Throws an ArgumentOutOfRangeException if index is outside the valid element range of span.

public static void ThrowIfIndexOutOfRange<T>(int index, ReadOnlySpan<T> span, string? paramName = null)

Parameters

index int

The index to validate.

span ReadOnlySpan<T>

The span against which to validate the index.

paramName string

The name of the index parameter. Supplied automatically by the compiler.

Type Parameters

T

The element type of the span.

Remarks

Uses an unsigned cast to collapse the two-sided bounds check into a single comparison. ReadOnlySpan<T> is a value type and cannot be null; no null guard is required or possible.

Exceptions

ArgumentOutOfRangeException

Thrown when index < 0 or >= span.Length.

ThrowIfIndexOutOfRange<T>(int, Span<T>, string?)

Throws an ArgumentOutOfRangeException if index is outside the valid element range of span.

public static void ThrowIfIndexOutOfRange<T>(int index, Span<T> span, string? paramName = null)

Parameters

index int

The index to validate.

span Span<T>

The span against which to validate the index.

paramName string

The name of the index parameter. Supplied automatically by the compiler.

Type Parameters

T

The element type of the span.

Remarks

Uses an unsigned cast to collapse the two-sided bounds check into a single comparison. Span<T> is a value type and cannot be null; no null guard is required or possible.

Exceptions

ArgumentOutOfRangeException

Thrown when index < 0 or >= span.Length.

ThrowIfInvalidStringComparison(StringComparison, string?)

Throws an ArgumentException if comparison is not a valid StringComparison value.

public static void ThrowIfInvalidStringComparison(StringComparison comparison, string? paramName = null)

Parameters

comparison StringComparison

The StringComparison value to validate.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Exceptions

ArgumentException

Thrown when comparison is not a defined enum member.

ThrowIfLessThanOrEqualOther<T>(T, T, string?, string?)

Throws an ArgumentException if value is less than or equal to other.

public static void ThrowIfLessThanOrEqualOther<T>(T value, T other, string? paramName = null, string? otherName = null) where T : IComparable<T>

Parameters

value T

The value being validated.

other T

The comparison reference value.

paramName string

The name of the value parameter. Supplied automatically by the compiler.

otherName string

The name of the comparison parameter. Supplied automatically by the compiler.

Type Parameters

T

A comparable type.

Exceptions

ArgumentException

Thrown when value <= other.

ThrowIfLessThanOrEqual<T>(T, T, string?)

Throws an ArgumentOutOfRangeException if value is less than or equal to min.

public static void ThrowIfLessThanOrEqual<T>(T value, T min, string? paramName = null) where T : IComparable<T>

Parameters

value T

The value to validate.

min T

The exclusive lower bound.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

A comparable type.

Exceptions

ArgumentOutOfRangeException

Thrown when value <= min.

ThrowIfLessThanOther<T>(T, T, string?, string?)

Throws an ArgumentException if value is less than other.

public static void ThrowIfLessThanOther<T>(T value, T other, string? paramName = null, string? otherName = null) where T : IComparable<T>

Parameters

value T

The value being validated.

other T

The comparison reference value.

paramName string

The name of the value parameter. Supplied automatically by the compiler.

otherName string

The name of the comparison parameter. Supplied automatically by the compiler.

Type Parameters

T

A comparable type.

Exceptions

ArgumentException

Thrown when value < other.

ThrowIfLessThan<T>(T?, T, bool, string?)

Throws an ArgumentOutOfRangeException if the nullable value is less than min. Optionally throws ArgumentNullException if value is null.

public static void ThrowIfLessThan<T>(T? value, T min, bool throwIfNull = false, string? paramName = null) where T : struct, IComparable<T>

Parameters

value T?

The nullable value to validate.

min T

The inclusive lower bound.

throwIfNull bool

When true, throws ArgumentNullException if value is null. When false, a null value passes validation.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

A comparable value type.

Exceptions

ArgumentNullException

Thrown when value is null and throwIfNull is true.

ArgumentOutOfRangeException

Thrown when value is non-null and less than min.

ThrowIfLessThan<T>(T, T, string?)

Throws an ArgumentOutOfRangeException if value is less than min.

public static void ThrowIfLessThan<T>(T value, T min, string? paramName = null) where T : IComparable<T>

Parameters

value T

The value to validate.

min T

The inclusive lower bound.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

A comparable type.

Exceptions

ArgumentOutOfRangeException

Thrown when value < min.

ThrowIfMultiplyOverflows(int, int, string?)

Throws an ArgumentOutOfRangeException if the product of left and right would fall outside the int range.

public static void ThrowIfMultiplyOverflows(int left, int right, string? paramName = null)

Parameters

left int

The left factor.

right int

The right factor.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Remarks

Guards input-controlled size or offset arithmetic against silent wrap-around before the result is used to index, slice, or allocate. The check is performed in a wider type so no overflow occurs during validation.

Exceptions

ArgumentOutOfRangeException

Thrown when left * right would overflow or underflow the int range.

ThrowIfMultiplyOverflows(long, long, string?)

Throws an ArgumentOutOfRangeException if the product of left and right would fall outside the long range.

public static void ThrowIfMultiplyOverflows(long left, long right, string? paramName = null)

Parameters

left long

The left factor.

right long

The right factor.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Remarks

Guards input-controlled size or offset arithmetic against silent wrap-around before the result is used to index, slice, or allocate. The check is performed in a wider type so no overflow occurs during validation.

Exceptions

ArgumentOutOfRangeException

Thrown when left * right would overflow or underflow the long range.

ThrowIfNegative<T>(T, string?)

Throws an ArgumentOutOfRangeException if value is negative.

public static void ThrowIfNegative<T>(T value, string? paramName = null) where T : struct, IComparable<T>

Parameters

value T

The value to validate.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

A comparable numeric type.

Exceptions

ArgumentOutOfRangeException

Thrown when value < 0.

ThrowIfNotAsciiAlphanumericUppercase(char, string?)

Throws an ArgumentOutOfRangeException if value is not an ASCII uppercase alphanumeric character, that is, a character in the range '0' (U+0030) to '9' (U+0039) or 'A' (U+0041) to 'Z' (U+005A) inclusive.

public static void ThrowIfNotAsciiAlphanumericUppercase(char value, string? paramName = null)

Parameters

value char

The character to validate.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Remarks

Useful for validating inputs to algorithms that operate on uppercase alphanumeric identifiers, such as ISO 7064 MOD 97-10 (IBAN / LEI), ISIN, SEDOL, and CUSIP. Lowercase letters are not accepted - the caller is expected to normalize before validation.

Exceptions

ArgumentOutOfRangeException

Thrown when value is outside the inclusive ranges '0' to '9' and 'A' to 'Z'.

ThrowIfNotAsciiDecimalDigit(char, string?)

Throws an ArgumentOutOfRangeException if value is not an ASCII decimal digit character, that is, a character in the range '0' (U+0030) to '9' (U+0039) inclusive.

public static void ThrowIfNotAsciiDecimalDigit(char value, string? paramName = null)

Parameters

value char

The character to validate.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Remarks

Useful for validating inputs to algorithms that operate on decimal digit strings, such as check-digit algorithms.

Exceptions

ArgumentOutOfRangeException

Thrown when value is outside the inclusive range '0' to '9'.

ThrowIfNotAsciiHexDigit(char, string?)

Throws an ArgumentOutOfRangeException if value is not an ASCII hexadecimal digit character - that is, a character in the ranges '0'-'9' (U+0030-U+0039), 'A'- 'F' (U+0041-U+0046), or 'a'-'f' (U+0061-U+0066) inclusive.

public static void ThrowIfNotAsciiHexDigit(char value, string? paramName = null)

Parameters

value char

The character to validate.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Remarks

Both upper- and lowercase hex letters are accepted. Useful for validating hex-encoded cryptographic outputs such as hash digests, key material, or initialization vectors prior to parsing.

Exceptions

ArgumentOutOfRangeException

Thrown when value is not a decimal digit or a letter in 'A'-'F' / 'a'- 'f'.

ThrowIfNotEqual<T>(T, T, string?)

Throws an ArgumentException if value does not equal other .

public static void ThrowIfNotEqual<T>(T value, T other, string? paramName = null) where T : IEquatable<T>

Parameters

value T

The value to validate.

other T

The value that value must equal.

paramName string

The name of the value parameter. Supplied automatically by the compiler.

Type Parameters

T

A type that implements IEquatable<T>.

Exceptions

ArgumentOutOfRangeException

Thrown when value does not equal other.

ThrowIfNotFinite(double, string?)

public static void ThrowIfNotFinite(double value, string? paramName = null)

Parameters

value double

The value to validate.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Exceptions

ArgumentOutOfRangeException

Thrown when value is not a finite number.

ThrowIfNotFinite(float, string?)

public static void ThrowIfNotFinite(float value, string? paramName = null)

Parameters

value float

The value to validate.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Exceptions

ArgumentOutOfRangeException

Thrown when value is not a finite number.

ThrowIfNotOfType<T>(object?, string?)

Throws an ArgumentException if value is not assignable to T.

public static void ThrowIfNotOfType<T>(object? value, string? paramName = null)

Parameters

value object

The object to check.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

The target type to validate against.

Remarks

A null value passes validation only when T is a reference or nullable type.

Exceptions

ArgumentException

Thrown when value is not null and not of type T, or when value is null and T is a non-nullable value type.

ThrowIfNotPositiveMultipleOf<T>(T, T, string?)

Throws an ArgumentOutOfRangeException if value is not a positive multiple of divisor.

public static void ThrowIfNotPositiveMultipleOf<T>(T value, T divisor, string? paramName = null) where T : IBinaryInteger<T>

Parameters

value T

The value to validate.

divisor T

The required positive divisor. Must itself be positive.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

A binary integer type.

Remarks

Useful for validating aligned buffer sizes, memory boundaries, or block-aligned lengths. When the required constraint is specifically a power of two, prefer ThrowIfNotPowerOfTwo<T>(T, string?), which uses a single bitwise operation.

Exceptions

ArgumentOutOfRangeException

Thrown when value <= 0 or value % divisor != 0.

ThrowIfNotPowerOfTwo<T>(T, string?)

Throws an ArgumentOutOfRangeException if value is not a positive power of two.

public static void ThrowIfNotPowerOfTwo<T>(T value, string? paramName = null) where T : IBinaryInteger<T>

Parameters

value T

The value to validate.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

A binary integer type.

Remarks

A specialization of the positive-multiple-of family. When the divisor is itself an arbitrary positive integer rather than a power of two, use ThrowIfNotPositiveMultipleOf<T>(T, T, string?) instead.

Exceptions

ArgumentOutOfRangeException

Thrown when value is not a positive integer whose binary representation contains exactly one set bit (i.e. value <= 0 or (value & (value - 1)) != 0).

ThrowIfNotZero<T>(T, string?)

Throws an ArgumentOutOfRangeException if value is not zero.

public static void ThrowIfNotZero<T>(T value, string? paramName = null) where T : struct, IEquatable<T>

Parameters

value T

The value to validate.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

A type that implements IEquatable<T>.

Exceptions

ArgumentOutOfRangeException

Thrown when value is not equal to the default value of T.

ThrowIfNullOrEmpty(string, string?)

Throws an ArgumentNullException if value is null, or an ArgumentException if it is an empty string.

public static void ThrowIfNullOrEmpty(string value, string? paramName = null)

Parameters

value string

The string value to validate.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Exceptions

ArgumentNullException

Thrown when value is null.

ArgumentException

Thrown when value is an empty string.

ThrowIfNullOrWhiteSpace(string, string?)

Throws an ArgumentNullException if value is null, or an ArgumentException if it is empty or contains only whitespace.

public static void ThrowIfNullOrWhiteSpace(string value, string? paramName = null)

Parameters

value string

The string value to validate.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Exceptions

ArgumentNullException

Thrown when value is null.

ArgumentException

Thrown when value is empty or contains only whitespace characters.

ThrowIfNull<T>(T, string?)

Throws an ArgumentNullException if value is null.

public static void ThrowIfNull<T>(T value, string? paramName = null)

Parameters

value T

The value to check. Must not be null.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

The type of the object.

Exceptions

ArgumentNullException

Thrown when value is null.

ThrowIfNull<T>(T, string, string?)

Throws an ArgumentNullException if value is null, using message as the exception message.

public static void ThrowIfNull<T>(T value, string message, string? paramName = null)

Parameters

value T

The value to check. Must not be null.

message string

The message to include in the exception.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

The type of the object.

Exceptions

ArgumentNullException

Thrown when value is null.

ThrowIfOutOfRange<T>(T, T, T, bool, string?)

Throws an ArgumentOutOfRangeException if value is not within the specified range.

public static void ThrowIfOutOfRange<T>(T value, T min, T max, bool inclusive = true, string? paramName = null) where T : IComparable<T>

Parameters

value T

The value to validate.

min T

The lower bound of the range.

max T

The upper bound of the range.

inclusive bool

When true, the bounds are inclusive; when false, they are exclusive.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

A type that implements IComparable<T>.

Exceptions

ArgumentOutOfRangeException

Thrown when value falls outside the range defined by min and max.

ThrowIfPositive<T>(T, string?)

Throws an ArgumentOutOfRangeException if value is positive.

public static void ThrowIfPositive<T>(T value, string? paramName = null) where T : struct, IComparable<T>

Parameters

value T

The value to validate.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

A comparable numeric type.

Exceptions

ArgumentOutOfRangeException

Thrown when value > 0.

ThrowIfReadOnly<T>(ICollection<T>, string?)

Throws an ArgumentNullException if collection is null, or an ArgumentException if it is read-only.

public static void ThrowIfReadOnly<T>(ICollection<T> collection, string? paramName = null)

Parameters

collection ICollection<T>

The collection to validate. Must not be null.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

The element type of the collection.

Exceptions

ArgumentNullException

Thrown when collection is null.

ArgumentException

Thrown when collection.IsReadOnly is true.

ThrowIfSequenceRangeOverflows(int, int, string?)

Throws an ArgumentOutOfRangeException if the sequence starting at start with count elements would overflow MaxValue.

public static void ThrowIfSequenceRangeOverflows(int start, int count, string? paramName = null)

Parameters

start int

The starting value of the sequence.

count int

The number of values in the sequence.

paramName string

The name of the count parameter. Supplied automatically by the compiler.

Remarks

Prevents arithmetic overflow when generating int-based numeric sequences.

Exceptions

ArgumentOutOfRangeException

Thrown when start + count - 1 would exceed MaxValue.

ThrowIfSequenceRangeOverflows(long, int, string?)

Throws an ArgumentOutOfRangeException if the sequence starting at start with count elements would overflow MaxValue.

public static void ThrowIfSequenceRangeOverflows(long start, int count, string? paramName = null)

Parameters

start long

The starting value of the sequence.

count int

The number of values in the sequence.

paramName string

The name of the count parameter. Supplied automatically by the compiler.

Remarks

Prevents arithmetic overflow when generating long-based numeric sequences.

Exceptions

ArgumentOutOfRangeException

Thrown when start + count - 1 would exceed MaxValue.

ThrowIfSpanLengthIsInsufficient<T>(ReadOnlySpan<T>, int, int, string?)

Throws an ArgumentException if the remaining elements of span from index are fewer than requiredLength.

public static void ThrowIfSpanLengthIsInsufficient<T>(ReadOnlySpan<T> span, int index, int requiredLength, string? paramName = null)

Parameters

span ReadOnlySpan<T>

The span to validate.

index int

The position from which remaining length is measured.

requiredLength int

The minimum number of elements required from index.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

The element type of the span.

Exceptions

ArgumentException

Thrown when span.Length - index < requiredLength.

ThrowIfSpanLengthIsInsufficient<T>(ReadOnlySpan<T>, int, string?)

Throws an exception if the specified span has fewer than minimumLength elements.

public static void ThrowIfSpanLengthIsInsufficient<T>(ReadOnlySpan<T> span, int minimumLength, string? paramName = null)

Parameters

span ReadOnlySpan<T>

The read-only span to validate.

minimumLength int

The minimum number of elements that span must contain.

paramName string

The name of the span parameter. Supplied automatically by the compiler via CallerArgumentExpressionAttribute.

Type Parameters

T

The element type of the span.

Remarks

ReadOnlySpan<T> is a value type and cannot be null; no null guard is required or possible. Use this overload when the caller may supply a larger span than required and the excess elements are simply ignored - for example, a buffer that must hold at least a full cipher block but may be larger. When the length must be exact, use ThrowIfSpanLengthIsNotEqualTo<T>(ReadOnlySpan<T>, int, string?) instead.

Exceptions

ArgumentException

Thrown when span.Length is less than minimumLength.

ThrowIfSpanLengthIsInsufficient<T>(Span<T>, int, int, string?)

Throws an ArgumentException if the remaining elements of span from index are fewer than requiredLength.

public static void ThrowIfSpanLengthIsInsufficient<T>(Span<T> span, int index, int requiredLength, string? paramName = null)

Parameters

span Span<T>

The span to validate.

index int

The position from which remaining length is measured.

requiredLength int

The minimum number of elements required from index.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

The element type of the span.

Exceptions

ArgumentException

Thrown when span.Length - index < requiredLength.

ThrowIfSpanLengthIsInsufficient<T>(Span<T>, int, string?)

Throws an exception if the specified span has fewer than minimumLength elements.

public static void ThrowIfSpanLengthIsInsufficient<T>(Span<T> span, int minimumLength, string? paramName = null)

Parameters

span Span<T>

The span to validate.

minimumLength int

The minimum number of elements that span must contain.

paramName string

The name of the span parameter. Supplied automatically by the compiler via CallerArgumentExpressionAttribute.

Type Parameters

T

The element type of the span.

Remarks

Span<T> is a value type and cannot be null; no null guard is required or possible. Use this overload when the caller may supply a larger span than required and the excess elements are simply ignored - for example, a buffer that must hold at least a full cipher block but may be larger. When the length must be exact, use ThrowIfSpanLengthIsNotEqualTo<T>(Span<T>, int, string?) instead.

Exceptions

ArgumentException

Thrown when span.Length is less than minimumLength.

ThrowIfSpanLengthIsNotEqualTo<T>(ReadOnlySpan<T>, int, string?)

Throws an exception if the span does not have exactly expectedLength elements.

public static void ThrowIfSpanLengthIsNotEqualTo<T>(ReadOnlySpan<T> span, int expectedLength, string? paramName = null)

Parameters

span ReadOnlySpan<T>

The span to validate.

expectedLength int

The exact number of elements that span must contain.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

The element type of the span.

Exceptions

ArgumentException

Thrown when span.Length does not equal expectedLength.

ThrowIfSpanLengthIsNotEqualTo<T>(Span<T>, int, string?)

Throws an exception if the span does not have exactly expectedLength elements.

public static void ThrowIfSpanLengthIsNotEqualTo<T>(Span<T> span, int expectedLength, string? paramName = null)

Parameters

span Span<T>

The span to validate.

expectedLength int

The exact number of elements that span must contain.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

The element type of the span.

Exceptions

ArgumentException

Thrown when span.Length does not equal expectedLength.

ThrowIfSpanLengthNotPositiveMultipleOf<T>(ReadOnlySpan<T>, int, bool, string?)

Throws an ArgumentException if the span length is not a positive multiple of divisor.

public static void ThrowIfSpanLengthNotPositiveMultipleOf<T>(ReadOnlySpan<T> span, int divisor, bool throwIfZero = true, string? paramName = null)

Parameters

span ReadOnlySpan<T>

The span to validate.

divisor int

The required positive divisor.

throwIfZero bool

When true, an empty span is treated as invalid. When false, an empty span passes validation.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

The element type of the span.

Exceptions

ArgumentException

Thrown when span is empty (and throwIfZero is true), or when span.Length % divisor != 0.

ThrowIfSpanLengthNotPositiveMultipleOf<T>(Span<T>, int, bool, string?)

Throws an ArgumentException if the span length is not a positive multiple of divisor.

public static void ThrowIfSpanLengthNotPositiveMultipleOf<T>(Span<T> span, int divisor, bool throwIfZero = true, string? paramName = null)

Parameters

span Span<T>

The span to validate.

divisor int

The required positive divisor.

throwIfZero bool

When true, an empty span is treated as invalid. When false, an empty span passes validation.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

The element type of the span.

Exceptions

ArgumentException

Thrown when span is empty (and throwIfZero is true), or when span.Length % divisor != 0.

ThrowIfSpanLengthOutOfRange<T>(ReadOnlySpan<T>, int, int, string?)

Throws an ArgumentOutOfRangeException if the span length is not between minLength and maxLength (inclusive).

public static void ThrowIfSpanLengthOutOfRange<T>(ReadOnlySpan<T> span, int minLength, int maxLength, string? paramName = null)

Parameters

span ReadOnlySpan<T>

The span to validate.

minLength int

The minimum permitted length, inclusive. Must be zero or greater.

maxLength int

The maximum permitted length, inclusive. Must be greater than or equal to minLength.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

The element type of the span.

Remarks

ReadOnlySpan<T> is a value type and cannot be null; no null guard is required or possible.

Exceptions

ArgumentOutOfRangeException

Thrown when span.Length < minLength or span.Length > maxLength.

ThrowIfSpanLengthOutOfRange<T>(Span<T>, int, int, string?)

Throws an ArgumentOutOfRangeException if the span length is not between minLength and maxLength (inclusive).

public static void ThrowIfSpanLengthOutOfRange<T>(Span<T> span, int minLength, int maxLength, string? paramName = null)

Parameters

span Span<T>

The span to validate.

minLength int

The minimum permitted length, inclusive. Must be zero or greater.

maxLength int

The maximum permitted length, inclusive. Must be greater than or equal to minLength.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

The element type of the span.

Remarks

Span<T> is a value type and cannot be null; no null guard is required or possible.

Exceptions

ArgumentOutOfRangeException

Thrown when span.Length < minLength or span.Length > maxLength.

ThrowIfSpanOffsetOrCountInvalid<T>(ReadOnlySpan<T>, int, int, string?, string?, string?)

Throws an ArgumentOutOfRangeException if offset or count is out of range, or an ArgumentException if the segment they define exceeds the bounds of span .

public static void ThrowIfSpanOffsetOrCountInvalid<T>(ReadOnlySpan<T> span, int offset, int count, string? paramSpanName = null, string? paramOffsetName = null, string? paramCountName = null)

Parameters

span ReadOnlySpan<T>

The read-only span to validate.

offset int

The zero-based starting index within the span.

count int

The number of elements to access from offset.

paramSpanName string

The name of the span parameter. Supplied automatically by the compiler via CallerArgumentExpressionAttribute.

paramOffsetName string

The name of the index parameter. Supplied automatically by the compiler via CallerArgumentExpressionAttribute.

paramCountName string

The name of the count parameter. Supplied automatically by the compiler via CallerArgumentExpressionAttribute.

Type Parameters

T

The type of elements in the span.

Remarks

This is the canonical implementation. The Span<T> overload ( ThrowIfSpanOffsetOrCountInvalid<T>(Span<T>, int, int, string?, string?, string?) ) converts its argument and delegates here, forwarding all CallerArgumentExpressionAttribute values explicitly so that exception messages always reflect the original call-site expressions.

Unlike the array equivalent (ThrowIfArrayOffsetOrCountInvalid), this overload does not check for null and does not throw ArgumentNullException . ReadOnlySpan<T> is a value type and can never be null; a default ReadOnlySpan<T> is equivalent to an empty span with Length of zero.

Although the index and count validations are expressed as two-part conditions (< 0 || > span.Length ) rather than the single unsigned-cast trick used in SpanExtensions, the explicit form is preferred here because this is a guard method whose primary purpose is clarity of intent. The unsigned-cast form trades readability for a marginal branch reduction that is irrelevant on an error path.

Exceptions

ArgumentOutOfRangeException

Thrown when offset or count is negative or exceeds Length.

ArgumentException

Thrown when index + count exceeds Length.

ThrowIfSpanOffsetOrCountInvalid<T>(Span<T>, int, int, string?, string?, string?)

Throws an ArgumentOutOfRangeException if offset or count is out of range, or an ArgumentException if the segment they define exceeds the bounds of span .

public static void ThrowIfSpanOffsetOrCountInvalid<T>(Span<T> span, int offset, int count, string? paramSpanName = null, string? paramIndexName = null, string? paramCountName = null)

Parameters

span Span<T>

The span to validate.

offset int

The zero-based starting index within the span.

count int

The number of elements to access from offset.

paramSpanName string

The name of the span parameter. Supplied automatically by the compiler via CallerArgumentExpressionAttribute.

paramIndexName string

The name of the index parameter. Supplied automatically by the compiler via CallerArgumentExpressionAttribute.

paramCountName string

The name of the count parameter. Supplied automatically by the compiler via CallerArgumentExpressionAttribute.

Type Parameters

T

The type of elements in the span.

Remarks

Unlike the array equivalent (ThrowIfArrayOffsetOrCountInvalid), this overload does not check for null and does not throw ArgumentNullException . Span<T> is a value type and can never be null; a default Span<T> is equivalent to an empty span with Length of zero.

Implicitly converts span to ReadOnlySpan<T> and delegates to ThrowIfSpanOffsetOrCountInvalid<T>(ReadOnlySpan<T>, int, int, string?, string?, string?), which is the canonical implementation. paramSpanName, paramIndexName, and paramCountName are forwarded explicitly to preserve the call-site argument expressions in any exception messages.

Exceptions

ArgumentOutOfRangeException

Thrown when offset or count is negative or exceeds Length.

ArgumentException

Thrown when index + count exceeds Length.

ThrowIfStreamNotReadable(Stream, string?)

Throws an ArgumentNullException if stream is null, or an ArgumentException if it does not support reading.

public static void ThrowIfStreamNotReadable(Stream stream, string? paramName = null)

Parameters

stream Stream

The stream to validate.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Exceptions

ArgumentNullException

Thrown when stream is null.

ArgumentException

Thrown when stream.CanRead is false.

ThrowIfStreamNotWritable(Stream, string?)

Throws an ArgumentNullException if stream is null, or an ArgumentException if it does not support writing.

public static void ThrowIfStreamNotWritable(Stream stream, string? paramName = null)

Parameters

stream Stream

The stream to validate.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Exceptions

ArgumentNullException

Thrown when stream is null.

ArgumentException

Thrown when stream.CanWrite is false.

ThrowIfStringLengthIsNotEqualTo(string, int, string?)

Throws an ArgumentNullException if value is null, or an ArgumentException if its length does not equal expectedLength characters.

public static void ThrowIfStringLengthIsNotEqualTo(string value, int expectedLength, string? paramName = null)

Parameters

value string

The string to validate. Must not be null.

expectedLength int

The exact required length in characters.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Remarks

Useful for validating fixed-length identifiers such as ISIN (12 characters), CUSIP (9 characters), or IBAN country codes (2 characters).

Exceptions

ArgumentNullException

Thrown when value is null.

ArgumentException

Thrown when value.Length != expectedLength.

ThrowIfStringLengthOutOfRange(string, int, int, string?)

Throws an ArgumentNullException if value is null, or an ArgumentOutOfRangeException if its length is not between minLength and maxLength (inclusive).

public static void ThrowIfStringLengthOutOfRange(string value, int minLength, int maxLength, string? paramName = null)

Parameters

value string

The string to validate. Must not be null.

minLength int

The minimum permitted length, inclusive.

maxLength int

The maximum permitted length, inclusive. Must be >= minLength.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Exceptions

ArgumentNullException

Thrown when value is null.

ArgumentOutOfRangeException

Thrown when value.Length < minLength or value.Length > maxLength.

ThrowIfStringTooLong(string, int, string?)

Throws an ArgumentNullException if value is null, or an ArgumentOutOfRangeException if its length exceeds maxLength characters.

public static void ThrowIfStringTooLong(string value, int maxLength, string? paramName = null)

Parameters

value string

The string to validate. Must not be null.

maxLength int

The maximum permitted length, inclusive.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Exceptions

ArgumentNullException

Thrown when value is null.

ArgumentOutOfRangeException

Thrown when value.Length > maxLength.

ThrowIfZeroOrNegative<T>(T, string?)

Throws an ArgumentOutOfRangeException if value is zero or negative.

public static void ThrowIfZeroOrNegative<T>(T value, string? paramName = null) where T : struct, IComparable<T>

Parameters

value T

The value to validate.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

A comparable numeric type.

Exceptions

ArgumentOutOfRangeException

Thrown when value <= 0.

ThrowIfZeroOrPositive<T>(T, string?)

Throws an ArgumentOutOfRangeException if value is zero or positive.

public static void ThrowIfZeroOrPositive<T>(T value, string? paramName = null) where T : struct, IComparable<T>

Parameters

value T

The value to validate.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

A comparable numeric type.

Exceptions

ArgumentOutOfRangeException

Thrown when value >= 0.

ThrowIfZero<T>(T, string?)

Throws an ArgumentOutOfRangeException if value equals zero.

public static void ThrowIfZero<T>(T value, string? paramName = null) where T : struct, IEquatable<T>

Parameters

value T

The value to validate.

paramName string

The name of the parameter. Supplied automatically by the compiler.

Type Parameters

T

A type that implements IEquatable<T>.

Exceptions

ArgumentOutOfRangeException

Thrown when value equals the default value of T.

Applies to

ProductVersions
.NET8, 10