Param Class
Provides methods to check the arguments of a method.
public static class ParamRemarks
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
| Name | Description |
|---|---|
| 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
valueReadOnlySpan<T>- The argument to check.
namestring- The name of the parameter with which
valuecorresponds. If you omit this argument, the compiler supplies the expression passed asvalue.
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.
Type parameters
T- The type of the items of
value.
Parameters
valueSpan<T>- The argument to check.
namestring- The name of the parameter with which
valuecorresponds. If you omit this argument, the compiler supplies the expression passed asvalue.
Returns
SpanParam<T> A SpanParam<T> that holds value and name.
Check<T>(T, string)
Starts a chain of checks on an argument.
CheckNotNull overloads
CheckNotNull<T>(T?, string)
Starts a chain of checks on an argument of a nullable value type that must not be null.
Type parameters
T- The underlying value type of
value.
Parameters
valueT?- The argument to check.
namestring- The name of the parameter with which
valuecorresponds. If you omit this argument, the compiler supplies the expression passed asvalue.
Returns
Param<T> A Param<T> that holds the underlying value of value, and name.
Exceptions
ArgumentNullExceptionvalueisnull.
CheckNotNull<T>(T?, string)
Starts a chain of checks on an argument that must not be null.
Type parameters
T- The type of
value.
Parameters
valueT?- The argument to check.
namestring- The name of the parameter with which
valuecorresponds. If you omit this argument, the compiler supplies the expression passed asvalue.
Returns
Param<T> A Param<T> that holds value and name.
Exceptions
ArgumentNullExceptionvalueisnull.
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.
Type parameters
T- The type of
value.
Parameters
valueT- The argument to check.
namestring- The name of the parameter with which
valuecorresponds. If you omit this argument, the compiler supplies the expression passed asvalue.
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
valueReadOnlySpan<char>- The argument to check.
namestring- The name of the parameter with which
valuecorresponds. If you omit this argument, the compiler supplies the expression passed asvalue.
Returns
ReadOnlySpan<char> The value of value, which is not empty.
Exceptions
ArgumentExceptionvalueis 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 : structType parameters
T- The underlying value type of
value.
Parameters
valueT?- The argument to check.
namestring- The name of the parameter with which
valuecorresponds. If you omit this argument, the compiler supplies the expression passed asvalue.
Returns
T The underlying value of value.
Exceptions
ArgumentNullExceptionvalueisnull.
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
valueT?- The argument to check.
namestring- The name of the parameter with which
valuecorresponds. If you omit this argument, the compiler supplies the expression passed asvalue.
Returns
T The value of value, which is not null.
Exceptions
ArgumentNullExceptionvalueisnull.
NotNullOrEmpty(string?, string)
Throws an exception if a string argument is null or empty.
Parameters
valuestring?- The argument to check.
namestring- The name of the parameter with which
valuecorresponds. If you omit this argument, the compiler supplies the expression passed asvalue.
Returns
string The value of value, which is neither null nor empty.
Exceptions
ArgumentNullExceptionvalueisnull.ArgumentExceptionvalueis the empty string.
NotNullOrWhiteSpace(string?, string)
Throws an exception if a string argument is null, empty, or consists only of white-space characters.
Parameters
valuestring?- The argument to check.
namestring- The name of the parameter with which
valuecorresponds. If you omit this argument, the compiler supplies the expression passed asvalue.
Returns
string The value of value, which holds at least one character that is not a white-space character.
Exceptions
ArgumentNullExceptionvalueisnull.ArgumentExceptionvalueis 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
valueReadOnlySpan<char>- The argument to check.
namestring- The name of the parameter with which
valuecorresponds. If you omit this argument, the compiler supplies the expression passed asvalue.
Returns
ReadOnlySpan<char> The value of value, which holds at least one character that is not a white-space character.
Exceptions
ArgumentExceptionvalueis empty, or consists only of white-space characters.