ThrowHelper Class
Definition
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
leftintThe left addend.
rightintThe right addend.
paramNamestringThe 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 + rightwould 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
leftlongThe left addend.
rightlongThe right addend.
paramNamestringThe 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 + rightwould 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
arrayArrayThe array to validate. Must not be null.
paramNamestringThe 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
arrayis null.- ArgumentException
Thrown when any element in
arrayis 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
arrayArrayThe array to validate. Must not be null.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Exceptions
- ArgumentNullException
Thrown when
arrayis 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
arrayArrayThe array to validate. Must not be null.
indexintThe zero-based starting index within the array.
requiredLengthintThe number of elements required starting from
index.paramNamestringThe name of the array parameter. Supplied automatically by the compiler via CallerArgumentExpressionAttribute.
indexParamNamestringThe 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
arrayis null.- ArgumentOutOfRangeException
Thrown when
indexis negative.- ArgumentException
Thrown when
array.Length - indexis less thanrequiredLength.
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
arrayArrayThe array to validate. Must not be null.
minimumLengthintThe minimum number of elements that
arraymust contain.paramNamestringThe 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
arrayis null.- ArgumentException
Thrown when
array.Lengthis less thanminimumLength.
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
arrayArrayThe array to validate. Must not be null.
expectedLengthintThe exact number of elements that
arraymust contain.paramNamestringThe 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
arrayis null.- ArgumentException
Thrown when
array.Lengthdoes not equalexpectedLength.
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
arrayArrayThe array to validate. Must not be null.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Exceptions
- ArgumentNullException
Thrown when
arrayis 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
arrayArrayThe array to validate. Must not be null.
divisorintThe required positive divisor.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Exceptions
- ArgumentNullException
Thrown when
arrayis null.- ArgumentException
Thrown when
array.Length == 0orarray.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
arrayArrayThe array to validate. Must not be null.
minLengthintThe minimum permitted length, inclusive. Must be zero or greater.
maxLengthintThe maximum permitted length, inclusive. Must be greater than or equal to
minLength.paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Exceptions
- ArgumentNullException
Thrown when
arrayis null.- ArgumentOutOfRangeException
Thrown when
array.Length < minLengthorarray.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
arrayArrayThe array to validate. Must not be null.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Exceptions
- ArgumentNullException
Thrown when
arrayis null.- ArgumentException
Thrown when
arrayhas 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
arrayArrayThe array to validate. Must not be null.
offsetintThe zero-based starting index within the array.
countintThe number of elements to access from
offset.paramArrayNamestringThe name of the array parameter. Supplied automatically by the compiler.
paramOffsetNamestringThe name of the index parameter. Supplied automatically by the compiler.
paramCountNamestringThe name of the count parameter. Supplied automatically by the compiler.
Exceptions
- ArgumentNullException
Thrown when
arrayis null.- ArgumentOutOfRangeException
Thrown when
offsetorcountis negative or exceedsarray.Length.- ArgumentException
Thrown when
index + countexceedsarray.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
arrayArrayThe array to validate. Must not be null.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TExpectedThe 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
arrayis null.- ArgumentException
Thrown when
arrayis not of typeTExpected[].
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
collectionICollection<T>The collection to validate. Must not be null.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TThe element type of the collection.
Exceptions
- ArgumentNullException
Thrown when
collectionis 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
collectionICollection<T>The collection to validate. Must not be null.
minCountintThe minimum number of required elements.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TThe element type of the collection.
Exceptions
- ArgumentNullException
Thrown when
collectionis 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
valueTValueThe parameter value to validate for null.
conditionalParamTConditionThe current value of the conditional parameter.
conditionalValueTConditionThe value of
conditionalParamthat makesvaluemandatory.paramNamestringThe name of the value parameter. Supplied automatically by the compiler.
conditionalParamNamestringThe name of the conditional parameter. Supplied automatically by the compiler.
Type Parameters
TValueThe type of the parameter being validated.
TConditionThe 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
conditionalParamequalsconditionalValueandvalueis 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
countintThe count value to validate.
availableintThe number of available items.
paramNamestringThe 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 orcount>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
sourceReadOnlySpan<TSource>The source span.
destinationSpan<TDestination>The destination span.
paramDestinationNamestringThe name of the destination parameter. Supplied automatically by the compiler.
Type Parameters
TSourceThe element type of the source span.
TDestinationThe 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
sourceTSource[]The source array. Must not be null.
destinationTDestination[]The destination array. Must not be null.
paramDestinationNamestringThe name of the destination parameter. Supplied automatically by the compiler.
Type Parameters
TSourceThe element type of the source array.
TDestinationThe element type of the destination array.
Remarks
Useful for validating array-to-array operations such as copying or endian-swapping.
Exceptions
- ArgumentNullException
Thrown when
sourceordestinationis 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
disposedboolThe disposal flag to evaluate.
objectNamestringThe name of the disposed object included in the exception message. Callers should pass this explicitly - typically
nameof(TypeName)orGetType().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
disposedis 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
valueTEnumThe value to validate.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TEnumThe enum type.
Exceptions
- ArgumentOutOfRangeException
Thrown when
valueis not a defined member ofTEnum.
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
valueTThe value to validate.
otherTThe value that
valuemust not equal.paramNamestringThe name of the value parameter. Supplied automatically by the compiler.
Type Parameters
TA type that implements IEquatable<T>.
Exceptions
- ArgumentOutOfRangeException
Thrown when
valueequalsother.
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
valueTThe value being validated.
otherTThe comparison reference value.
paramNamestringThe name of the value parameter. Supplied automatically by the compiler.
otherNamestringThe name of the comparison parameter. Supplied automatically by the compiler.
Type Parameters
TA 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
valueTThe value to compare.
maxTThe exclusive upper bound.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TA 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
valueTThe value being validated.
otherTThe comparison reference value.
paramNamestringThe name of the value parameter. Supplied automatically by the compiler.
otherNamestringThe name of the comparison parameter. Supplied automatically by the compiler.
Type Parameters
TA 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
valueTThe value to compare.
maxTThe inclusive upper bound.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TA 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
indexlongThe index to validate.
arrayArrayThe array against which to validate the index.
paramNamestringThe name of the index parameter. Supplied automatically by the compiler.
Exceptions
- ArgumentNullException
Thrown when
arrayis 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
indexintThe index to validate.
spanReadOnlySpan<T>The span against which to validate the index.
paramNamestringThe name of the index parameter. Supplied automatically by the compiler.
Type Parameters
TThe 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
indexintThe index to validate.
spanSpan<T>The span against which to validate the index.
paramNamestringThe name of the index parameter. Supplied automatically by the compiler.
Type Parameters
TThe 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
comparisonStringComparisonThe StringComparison value to validate.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Exceptions
- ArgumentException
Thrown when
comparisonis 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
valueTThe value being validated.
otherTThe comparison reference value.
paramNamestringThe name of the value parameter. Supplied automatically by the compiler.
otherNamestringThe name of the comparison parameter. Supplied automatically by the compiler.
Type Parameters
TA 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
valueTThe value to validate.
minTThe exclusive lower bound.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TA 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
valueTThe value being validated.
otherTThe comparison reference value.
paramNamestringThe name of the value parameter. Supplied automatically by the compiler.
otherNamestringThe name of the comparison parameter. Supplied automatically by the compiler.
Type Parameters
TA 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
valueT?The nullable value to validate.
minTThe inclusive lower bound.
throwIfNullboolWhen true, throws ArgumentNullException if
valueis null. When false, a null value passes validation.paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TA comparable value type.
Exceptions
- ArgumentNullException
- ArgumentOutOfRangeException
Thrown when
valueis non-null and less thanmin.
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
valueTThe value to validate.
minTThe inclusive lower bound.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TA 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
leftintThe left factor.
rightintThe right factor.
paramNamestringThe 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 * rightwould 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
leftlongThe left factor.
rightlongThe right factor.
paramNamestringThe 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 * rightwould 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
valueTThe value to validate.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TA 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
valuecharThe character to validate.
paramNamestringThe 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
valueis 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
valuecharThe character to validate.
paramNamestringThe 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
valueis 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
valuecharThe character to validate.
paramNamestringThe 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
valueis 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
valueTThe value to validate.
otherTThe value that
valuemust equal.paramNamestringThe name of the value parameter. Supplied automatically by the compiler.
Type Parameters
TA type that implements IEquatable<T>.
Exceptions
- ArgumentOutOfRangeException
Thrown when
valuedoes not equalother.
ThrowIfNotFinite(double, string?)
Throws an ArgumentOutOfRangeException if value is NaN,
PositiveInfinity, or NegativeInfinity.
public static void ThrowIfNotFinite(double value, string? paramName = null)
Parameters
valuedoubleThe value to validate.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Exceptions
- ArgumentOutOfRangeException
Thrown when
valueis not a finite number.
ThrowIfNotFinite(float, string?)
Throws an ArgumentOutOfRangeException if value is NaN,
PositiveInfinity, or NegativeInfinity.
public static void ThrowIfNotFinite(float value, string? paramName = null)
Parameters
valuefloatThe value to validate.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Exceptions
- ArgumentOutOfRangeException
Thrown when
valueis 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
valueobjectThe object to check.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TThe target type to validate against.
Remarks
A null value passes validation only when T is a reference or nullable
type.
Exceptions
- ArgumentException
Thrown when
valueis not null and not of typeT, or whenvalueis null andTis 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
valueTThe value to validate.
divisorTThe required positive divisor. Must itself be positive.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TA 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 orvalue % 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
valueTThe value to validate.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TA 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
valueis not a positive integer whose binary representation contains exactly one set bit (i.e.value <= 0or(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
valueTThe value to validate.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TA type that implements IEquatable<T>.
Exceptions
- ArgumentOutOfRangeException
Thrown when
valueis not equal to the default value ofT.
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
valuestringThe string value to validate.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Exceptions
- ArgumentNullException
Thrown when
valueis null.- ArgumentException
Thrown when
valueis 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
valuestringThe string value to validate.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Exceptions
- ArgumentNullException
Thrown when
valueis null.- ArgumentException
Thrown when
valueis 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
valueTThe value to check. Must not be null.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TThe type of the object.
Exceptions
- ArgumentNullException
Thrown when
valueis 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
valueTThe value to check. Must not be null.
messagestringThe message to include in the exception.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TThe type of the object.
Exceptions
- ArgumentNullException
Thrown when
valueis 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
valueTThe value to validate.
minTThe lower bound of the range.
maxTThe upper bound of the range.
inclusiveboolWhen true, the bounds are inclusive; when false, they are exclusive.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TA type that implements IComparable<T>.
Exceptions
- ArgumentOutOfRangeException
Thrown when
valuefalls outside the range defined byminandmax.
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
valueTThe value to validate.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TA 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
collectionICollection<T>The collection to validate. Must not be null.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TThe element type of the collection.
Exceptions
- ArgumentNullException
Thrown when
collectionis null.- ArgumentException
Thrown when
collection.IsReadOnlyis 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
startintThe starting value of the sequence.
countintThe number of values in the sequence.
paramNamestringThe 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 - 1would 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
startlongThe starting value of the sequence.
countintThe number of values in the sequence.
paramNamestringThe 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 - 1would 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
spanReadOnlySpan<T>The span to validate.
indexintThe position from which remaining length is measured.
requiredLengthintThe minimum number of elements required from
index.paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TThe 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
spanReadOnlySpan<T>The read-only span to validate.
minimumLengthintThe minimum number of elements that
spanmust contain.paramNamestringThe name of the span parameter. Supplied automatically by the compiler via CallerArgumentExpressionAttribute.
Type Parameters
TThe 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.Lengthis less thanminimumLength.
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
spanSpan<T>The span to validate.
indexintThe position from which remaining length is measured.
requiredLengthintThe minimum number of elements required from
index.paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TThe 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
spanSpan<T>The span to validate.
minimumLengthintThe minimum number of elements that
spanmust contain.paramNamestringThe name of the span parameter. Supplied automatically by the compiler via CallerArgumentExpressionAttribute.
Type Parameters
TThe 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.Lengthis less thanminimumLength.
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
spanReadOnlySpan<T>The span to validate.
expectedLengthintThe exact number of elements that
spanmust contain.paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TThe element type of the span.
Exceptions
- ArgumentException
Thrown when
span.Lengthdoes not equalexpectedLength.
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
spanSpan<T>The span to validate.
expectedLengthintThe exact number of elements that
spanmust contain.paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TThe element type of the span.
Exceptions
- ArgumentException
Thrown when
span.Lengthdoes not equalexpectedLength.
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
spanReadOnlySpan<T>The span to validate.
divisorintThe required positive divisor.
throwIfZeroboolWhen true, an empty span is treated as invalid. When false, an empty span passes validation.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TThe element type of the span.
Exceptions
- ArgumentException
Thrown when
spanis empty (andthrowIfZerois true), or whenspan.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
spanSpan<T>The span to validate.
divisorintThe required positive divisor.
throwIfZeroboolWhen true, an empty span is treated as invalid. When false, an empty span passes validation.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TThe element type of the span.
Exceptions
- ArgumentException
Thrown when
spanis empty (andthrowIfZerois true), or whenspan.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
spanReadOnlySpan<T>The span to validate.
minLengthintThe minimum permitted length, inclusive. Must be zero or greater.
maxLengthintThe maximum permitted length, inclusive. Must be greater than or equal to
minLength.paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TThe 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 < minLengthorspan.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
spanSpan<T>The span to validate.
minLengthintThe minimum permitted length, inclusive. Must be zero or greater.
maxLengthintThe maximum permitted length, inclusive. Must be greater than or equal to
minLength.paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TThe element type of the span.
Remarks
Exceptions
- ArgumentOutOfRangeException
Thrown when
span.Length < minLengthorspan.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
spanReadOnlySpan<T>The read-only span to validate.
offsetintThe zero-based starting index within the span.
countintThe number of elements to access from
offset.paramSpanNamestringThe name of the span parameter. Supplied automatically by the compiler via CallerArgumentExpressionAttribute.
paramOffsetNamestringThe name of the index parameter. Supplied automatically by the compiler via CallerArgumentExpressionAttribute.
paramCountNamestringThe name of the count parameter. Supplied automatically by the compiler via CallerArgumentExpressionAttribute.
Type Parameters
TThe 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
offsetorcountis negative or exceeds Length.- ArgumentException
Thrown when
index + countexceeds 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
spanSpan<T>The span to validate.
offsetintThe zero-based starting index within the span.
countintThe number of elements to access from
offset.paramSpanNamestringThe name of the span parameter. Supplied automatically by the compiler via CallerArgumentExpressionAttribute.
paramIndexNamestringThe name of the index parameter. Supplied automatically by the compiler via CallerArgumentExpressionAttribute.
paramCountNamestringThe name of the count parameter. Supplied automatically by the compiler via CallerArgumentExpressionAttribute.
Type Parameters
TThe 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
offsetorcountis negative or exceeds Length.- ArgumentException
Thrown when
index + countexceeds 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
streamStreamThe stream to validate.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Exceptions
- ArgumentNullException
Thrown when
streamis null.- ArgumentException
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
streamStreamThe stream to validate.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Exceptions
- ArgumentNullException
Thrown when
streamis null.- ArgumentException
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
valuestringThe string to validate. Must not be null.
expectedLengthintThe exact required length in characters.
paramNamestringThe 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
valueis 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
valuestringThe string to validate. Must not be null.
minLengthintThe minimum permitted length, inclusive.
maxLengthintThe maximum permitted length, inclusive. Must be >=
minLength.paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Exceptions
- ArgumentNullException
Thrown when
valueis null.- ArgumentOutOfRangeException
Thrown when
value.Length < minLengthorvalue.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
valuestringThe string to validate. Must not be null.
maxLengthintThe maximum permitted length, inclusive.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Exceptions
- ArgumentNullException
Thrown when
valueis 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
valueTThe value to validate.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TA 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
valueTThe value to validate.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TA 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
valueTThe value to validate.
paramNamestringThe name of the parameter. Supplied automatically by the compiler.
Type Parameters
TA type that implements IEquatable<T>.
Exceptions
- ArgumentOutOfRangeException
Thrown when
valueequals the default value ofT.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |