Param Class

Provides methods to check the arguments of a method.

public static class Param

Remarks

A check throws an exception if its argument is not valid.

A direct check, such as NotNull, returns the argument, so that a check and an assignment fit in one expression.

The Check and CheckNotNull methods start a chain of checks on an argument, and return a Param<T>. For a span, Check returns a ReadOnlySpanParam<T> or a SpanParam<T>. The Value property of each of these types ends the chain.

Every method takes the name of the parameter as its last argument. If you omit it, the compiler supplies the expression passed as the argument to check.

Example

The following code checks the arguments of a constructor:

public Customer(string name, Address address)
{
    Name = Param.NotNullOrWhiteSpace(name);
    Address = Param.NotNull(address);
}

Methods

NameDescription
Check<T>(ReadOnlySpan<T>, string)Starts a chain of checks on a read-only span argument.
Check<T>(Span<T>, string)Starts a chain of checks on a span argument.
Check<T>(T, string)Starts a chain of checks on an argument.
CheckNotNull<T>(T?, string)Starts a chain of checks on an argument of a nullable value type that must not be null.
CheckNotNull<T>(T?, string)Starts a chain of checks on an argument that must not be null.
CheckRef<T>(T, string)Starts a chain of checks on an argument whose type can be a ref struct.
NotEmpty(ReadOnlySpan<char>, string)Throws an ArgumentException if a span of characters is empty.
NotNull<T>(T?, string)Throws an ArgumentNullException if an argument of a nullable value type is null.
NotNull<T>(T?, string)Throws an ArgumentNullException if an argument is null.
NotNullOrEmpty(string?, string)Throws an exception if a string argument is null or empty.
NotNullOrWhiteSpace(string?, string)Throws an exception if a string argument is null, empty, or consists only of white-space characters.
NotWhiteSpace(ReadOnlySpan<char>, string)Throws an ArgumentException if a span of characters is empty, or consists only of white-space characters.

Check overloads

Check<T>(ReadOnlySpan<T>, string)

Starts a chain of checks on a read-only span argument.

public static ReadOnlySpanParam<T> Check<T>(ReadOnlySpan<T> value, string name = "")
Type parameters
T
The type of the items of value.
Parameters
value ReadOnlySpan<T>
The argument to check.
name string
The name of the parameter with which value corresponds. If you omit this argument, the compiler supplies the expression passed as value.
Returns

ReadOnlySpanParam<T> A ReadOnlySpanParam<T> that holds value and name.

Check<T>(Span<T>, string)

Starts a chain of checks on a span argument.

public static SpanParam<T> Check<T>(Span<T> value, string name = "")
Type parameters
T
The type of the items of value.
Parameters
value Span<T>
The argument to check.
name string
The name of the parameter with which value corresponds. If you omit this argument, the compiler supplies the expression passed as value.
Returns

SpanParam<T> A SpanParam<T> that holds value and name.

Check<T>(T, string)

Starts a chain of checks on an argument.

public static Param<T> Check<T>(T value, string name = "")
Type parameters
T
The type of value.
Parameters
value T
The argument to check.
name string
The name of the parameter with which value corresponds. If you omit this argument, the compiler supplies the expression passed as value.
Returns

Param<T> A Param<T> that holds value and name.

CheckNotNull overloads

CheckNotNull<T>(T?, string)

Starts a chain of checks on an argument of a nullable value type that must not be null.

public static Param<T> CheckNotNull<T>(T? value, string name = "") where T : struct
Type parameters
T
The underlying value type of value.
Parameters
value T?
The argument to check.
name string
The name of the parameter with which value corresponds. If you omit this argument, the compiler supplies the expression passed as value.
Returns

Param<T> A Param<T> that holds the underlying value of value, and name.

Exceptions
ArgumentNullException
value is null.

CheckNotNull<T>(T?, string)

Starts a chain of checks on an argument that must not be null.

public static Param<T> CheckNotNull<T>(T? value, string name = "")
Type parameters
T
The type of value.
Parameters
value T?
The argument to check.
name string
The name of the parameter with which value corresponds. If you omit this argument, the compiler supplies the expression passed as value.
Returns

Param<T> A Param<T> that holds value and name.

Exceptions
ArgumentNullException
value is null.

CheckRef<T>(T, string)

Available in: net10.0net11.0(not available in the other target frameworks of the package)

Starts a chain of checks on an argument whose type can be a ref struct.

public static RefParam<T> CheckRef<T>(T value, string name = "") where T : allows ref struct

Type parameters

T
The type of value.

Parameters

value T
The argument to check.
name string
The name of the parameter with which value corresponds. If you omit this argument, the compiler supplies the expression passed as value.

Returns

RefParam<T> A RefParam<T> that holds value and name.

Remarks

This method is not available in the netstandard and .NET Framework builds of Louis, because their BCL has no RuntimeFeature.ByRefLikeGenerics.

NotEmpty(ReadOnlySpan<char>, string)

Throws an ArgumentException if a span of characters is empty.

public static ReadOnlySpan<char> NotEmpty(ReadOnlySpan<char> value, string name = "")

Parameters

value ReadOnlySpan<char>
The argument to check.
name string
The name of the parameter with which value corresponds. If you omit this argument, the compiler supplies the expression passed as value.

Returns

ReadOnlySpan<char> The value of value, which is not empty.

Exceptions

ArgumentException
value is empty.

NotNull overloads

NotNull<T>(T?, string)

Throws an ArgumentNullException if an argument of a nullable value type is null.

public static T NotNull<T>(T? value, string name = "") where T : struct
Type parameters
T
The underlying value type of value.
Parameters
value T?
The argument to check.
name string
The name of the parameter with which value corresponds. If you omit this argument, the compiler supplies the expression passed as value.
Returns

T The underlying value of value.

Exceptions
ArgumentNullException
value is null.

NotNull<T>(T?, string)

Throws an ArgumentNullException if an argument is null.

public static T NotNull<T>(T? value, string name = "")
Type parameters
T
The type of value.
Parameters
value T?
The argument to check.
name string
The name of the parameter with which value corresponds. If you omit this argument, the compiler supplies the expression passed as value.
Returns

T The value of value, which is not null.

Exceptions
ArgumentNullException
value is null.

NotNullOrEmpty(string?, string)

Throws an exception if a string argument is null or empty.

public static string NotNullOrEmpty(string? value, string name = "")

Parameters

value string?
The argument to check.
name string
The name of the parameter with which value corresponds. If you omit this argument, the compiler supplies the expression passed as value.

Returns

string The value of value, which is neither null nor empty.

Exceptions

ArgumentNullException
value is null.
ArgumentException
value is the empty string.

NotNullOrWhiteSpace(string?, string)

Throws an exception if a string argument is null, empty, or consists only of white-space characters.

public static string NotNullOrWhiteSpace(string? value, string name = "")

Parameters

value string?
The argument to check.
name string
The name of the parameter with which value corresponds. If you omit this argument, the compiler supplies the expression passed as value.

Returns

string The value of value, which holds at least one character that is not a white-space character.

Exceptions

ArgumentNullException
value is null.
ArgumentException
value is the empty string, or consists only of white-space characters.

NotWhiteSpace(ReadOnlySpan<char>, string)

Throws an ArgumentException if a span of characters is empty, or consists only of white-space characters.

public static ReadOnlySpan<char> NotWhiteSpace(ReadOnlySpan<char> value, string name = "")

Parameters

value ReadOnlySpan<char>
The argument to check.
name string
The name of the parameter with which value corresponds. If you omit this argument, the compiler supplies the expression passed as value.

Returns

ReadOnlySpan<char> The value of value, which holds at least one character that is not a white-space character.

Exceptions

ArgumentException
value is empty, or consists only of white-space characters.