Fork me on GitHub
Search Results for

    Show / Hide Table of Contents

    Class Guard

    Provides guard clauses to validate method arguments, in order to enforce defensive programming practice. See defensive programming. See guard clauses.

    Inheritance
    object
    Guard
    Namespace: Krafted.Guards
    Syntax
    public class Guard

    Properties

    | Edit this page View Source

    Against

    The entry point to build the guard clauses.

    Declaration
    public static Guard Against { get; }
    Property Value
    Type Description
    Guard

    Methods

    | Edit this page View Source

    Any<T>(IEnumerable<T>?, string, string?)

    Throws an ArgumentException if the collection contains any elements, with this error message: Collection should be empty.

    Declaration
    public Guard Any<T>(IEnumerable<T>? collection, string message = "", string? parameterName = null)
    Parameters
    Type Name Description
    IEnumerable<T> collection

    The parameter to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which collection corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Type Parameters
    Name Description
    T

    The type of enumerable.

    Remarks

    If the collection is null, the validation is ignored (i.e. does not throws an ArgumentException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentException

    .

    | Edit this page View Source

    Empty(Guid?, string, string?)

    Throws an ArgumentException if the parameter is empty, with this error message: Guid cannot be empty.

    Declaration
    public Guard Empty(Guid? parameter, string message = "", string? parameterName = null)
    Parameters
    Type Name Description
    Guid? parameter

    The parameter to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which parameter corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Remarks

    If the parameter is null, the validation is ignored (i.e. does not throws an ArgumentException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentException

    .

    | Edit this page View Source

    EmptyOrWhiteSpace(string?, string, string?)

    Throws an ArgumentException if the parameter is an empty string or consists only of white-space, with this error message: Parameter cannot be empty or consists exclusively of white-space characters.

    Declaration
    public Guard EmptyOrWhiteSpace(string? parameter, string message = "", string? parameterName = null)
    Parameters
    Type Name Description
    string parameter

    The parameter to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which parameter corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Remarks

    If the parameter is null, the validation is ignored (i.e. does not throws an ArgumentException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentNullException

    parameter is null.

    | Edit this page View Source

    False(bool?, string, string?)

    Throws an ArgumentException if the parameter is false, with this error message: Parameter cannot be false.

    Declaration
    public Guard False(bool? parameter, string message = "", string? parameterName = null)
    Parameters
    Type Name Description
    bool? parameter

    The parameter to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which parameter corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Remarks

    If the parameter is null, the validation is ignored (i.e. does not throws an ArgumentException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentException

    .

    | Edit this page View Source

    False(Predicate<bool>?, string, string?)

    Throws an ArgumentException if the predicate is false, with this error message: Predicate cannot be false. Or, if the optional parameter message was set, with their message.

    Declaration
    public Guard False(Predicate<bool>? predicate, string message = "", string? parameterName = null)
    Parameters
    Type Name Description
    Predicate<bool> predicate

    The predicate to check.

    string message

    The optional error message passed to ArgumentException if the predicate is false.

    string parameterName

    The name of the parameter with which predicate corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Remarks

    If the predicate is null, the validation is ignored (i.e. does not throws an ArgumentException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentException

    .

    | Edit this page View Source

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

    Throws an ArgumentException if the number is greater than valueToCompare, with this error message: Number cannot be greater than {valueToCompare}.

    Declaration
    public Guard GreaterThan<T>(T? number, T valueToCompare, string message = "", string? parameterName = null) where T : struct, IComparable
    Parameters
    Type Name Description
    T? number

    The number to check.

    T valueToCompare

    The value to compare.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which valueToCompare corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Type Parameters
    Name Description
    T

    The type of the number.

    Remarks

    If the number is null, the validation is ignored (i.e. does not throws an ArgumentException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentException

    .

    | Edit this page View Source

    InvalidCnpj(string?, string)

    Throws an FormatException if the cnpj is invalid, with this error message: Invalid CNPJ: {cnpj}.

    CNPJ stands for "Cadastro Nacional da Pessoa Jurídica" in Brazil. It's used to identify a legal entity and other types of legal arrangement without legal personality (such as condominiums, public agencies, funds) in Brazil. See more.

    Declaration
    public Guard InvalidCnpj(string? cnpj, string message = "")
    Parameters
    Type Name Description
    string cnpj

    The cnpj to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    Returns
    Type Description
    Guard

    The guard.

    Remarks

    If the cnpj is null, the validation is ignored (i.e. does not throws an FormatException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    FormatException

    .

    | Edit this page View Source

    InvalidCpf(string?, string)

    Throws an FormatException if the cpf is invalid, with this error message: Invalid CPF: {cpf}.

    CPF stands for "Cadastro de Pessoa Física" in Brazil. It's used to identify an individual taxpayer in Brazil. See more.

    Declaration
    public Guard InvalidCpf(string? cpf, string message = "")
    Parameters
    Type Name Description
    string cpf

    The cpf to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    Returns
    Type Description
    Guard

    The guard.

    Remarks

    If the cpf is null, the validation is ignored (i.e. does not throws an FormatException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    FormatException

    .

    | Edit this page View Source

    InvalidEmail(string?, string, string?)

    Throws an FormatException if the emailAddress is invalid, with this error message: E-mail address invalid: {emailAddress}.

    Declaration
    public Guard InvalidEmail(string? emailAddress, string message = "", string? parameterName = null)
    Parameters
    Type Name Description
    string emailAddress

    The e-mail address to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which emailAddress corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Remarks

    If the emailAddress is null, the validation is ignored (i.e. does not throws an ArgumentException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    FormatException

    .

    | Edit this page View Source

    InvalidNif(string?, string)

    Throws an FormatException if the nif is invalid, with this error message: Invalid NIF: {nif}.

    NIF stands for "Número de Identificação Fiscal" in Portugal. It's used to identify an individual or legal entity taxpayer in Portugal. See more.

    Declaration
    public Guard InvalidNif(string? nif, string message = "")
    Parameters
    Type Name Description
    string nif

    The nif to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    Returns
    Type Description
    Guard

    The guard.

    Remarks

    If the nif is null, the validation is ignored (i.e. does not throws an FormatException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    FormatException

    .

    | Edit this page View Source

    Length(uint, string?, string, string?)

    Throws an ArgumentOutOfRangeException if the length of the parameter is different of the specified exactLength, with this error message: parameterName must be length exactLength.

    Declaration
    public Guard Length(uint exactLength, string? parameter, string message = "", string? parameterName = null)
    Parameters
    Type Name Description
    uint exactLength

    The exact length the parameter must be.

    string parameter

    The parameter to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which parameter corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Remarks

    If the parameter is null, the validation is ignored (i.e. does not throws an ArgumentOutOfRangeException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentOutOfRangeException

    .

    | Edit this page View Source

    Length(uint, uint, string?, string, string?)

    Throws an ArgumentOutOfRangeException if the length of the parameter is outside of the specified range, with this error message: parameterName must be at least minLength character, and at most maxLength characters.

    Declaration
    public Guard Length(uint minLength, uint maxLength, string? parameter, string message = "", string? parameterName = null)
    Parameters
    Type Name Description
    uint minLength

    The minimum length the parameter must be.

    uint maxLength

    The maximum length the parameter must be.

    string parameter

    The parameter to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which parameter corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Remarks

    If the parameter is null, the validation is ignored (i.e. does not throws an ArgumentOutOfRangeException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentOutOfRangeException

    .

    | Edit this page View Source

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

    Throws an ArgumentException if the number is less than valueToCompare, with this error message: Number cannot be less than {valueToCompare}.

    Declaration
    public Guard LessThan<T>(T? number, T valueToCompare, string message = "", string? parameterName = null) where T : struct, IComparable
    Parameters
    Type Name Description
    T? number

    The number to check.

    T valueToCompare

    The value to compare.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which valueToCompare corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Type Parameters
    Name Description
    T

    The type of the number.

    Remarks

    If the number is null, the validation is ignored (i.e. does not throws an ArgumentException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentException

    .

    | Edit this page View Source

    Match(string?, string, RegexOptions, string, string?)

    Throws an ArgumentException if the regular expression specified in pattern finds a match in parameter, with this error message: Parameter matches the specified regular expression.

    Declaration
    public Guard Match(string? parameter, string pattern, RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Compiled, string message = "", string? parameterName = null)
    Parameters
    Type Name Description
    string parameter

    The parameter to check.

    string pattern

    The regular expression pattern to match.

    RegexOptions options

    The regular expression options.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which parameter corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Remarks

    If the parameter is null, the validation is ignored (i.e. does not throws an ArgumentException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentException

    .

    | Edit this page View Source

    Match<TException>(string?, string, RegexOptions, string, string?)

    Throws an ArgumentException if the regular expression specified in pattern finds a match in parameter, with this error message: Parameter matches the specified regular expression.

    Declaration
    public Guard Match<TException>(string? parameter, string pattern, RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Compiled, string message = "", string? parameterName = null) where TException : Exception
    Parameters
    Type Name Description
    string parameter

    The parameter to check.

    string pattern

    The regular expression pattern to match.

    RegexOptions options

    The regular expression options.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which parameter corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Type Parameters
    Name Description
    TException

    The type of the exception.

    Remarks

    If the parameter is null, the validation is ignored (i.e. does not throws an TException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentException

    .

    | Edit this page View Source

    MaxLength(uint, string?, string, string?)

    Throws an ArgumentException if the length of the parameter is greater than the specified maxLength, with this error message: parameterName must be at most maxLength characters.

    Declaration
    public Guard MaxLength(uint maxLength, string? parameter, string message = "", string? parameterName = null)
    Parameters
    Type Name Description
    uint maxLength

    The maximum length the parameter must be.

    string parameter

    The parameter to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which parameter corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Remarks

    If the parameter is null, the validation is ignored (i.e. does not throws an ArgumentException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentException

    .

    | Edit this page View Source

    MinLength(uint, string?, string, string?)

    Throws an ArgumentException if the length of the parameter is less than the specified minLength, with this error message: parameterName must be at least minLength characters.

    Declaration
    public Guard MinLength(uint minLength, string? parameter, string message = "", string? parameterName = null)
    Parameters
    Type Name Description
    uint minLength

    The minimum length the parameter must be.

    string parameter

    The parameter to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which parameter corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Remarks

    If the parameter is null, the validation is ignored (i.e. does not throws an ArgumentException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentException

    .

    | Edit this page View Source

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

    Throws an ArgumentException if the number is negative, with this error message: Number cannot be negative.

    Declaration
    public Guard Negative<T>(T? number, string message = "", string? parameterName = null) where T : struct, IComparable
    Parameters
    Type Name Description
    T? number

    The number to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which number corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Type Parameters
    Name Description
    T

    The type of the number.

    Remarks

    If the number is null, the validation is ignored (i.e. does not throws an ArgumentException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentException

    .

    | Edit this page View Source

    NotAny<T>(IEnumerable<T>?, string, string?)

    Throws an ArgumentException if the collection not contains any elements, with this error message: Collection cannot be empty.

    Declaration
    public Guard NotAny<T>(IEnumerable<T>? collection, string message = "", string? parameterName = null)
    Parameters
    Type Name Description
    IEnumerable<T> collection

    The parameter to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which collection corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Type Parameters
    Name Description
    T

    The type of enumerable.

    Remarks

    If the collection is null, the validation is ignored (i.e. does not throws an ArgumentException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentException

    .

    | Edit this page View Source

    NotEmpty(Guid?, string, string?)

    Throws an ArgumentException if the parameter is not empty, with this error message: Guid should be empty.

    Declaration
    public Guard NotEmpty(Guid? parameter, string message = "", string? parameterName = null)
    Parameters
    Type Name Description
    Guid? parameter

    The parameter to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which parameter corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Remarks

    If the parameter is null, the validation is ignored (i.e. does not throws an ArgumentException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentException

    .

    | Edit this page View Source

    NotExists<TEnum>(TEnum?, string, string?)

    Throws an ArgumentException if the specified value not exists in the specified TEnum enumeration, with this error message: The {value} not exists in the {TEnum}.

    Declaration
    public Guard NotExists<TEnum>(TEnum? value, string message = "", string? parameterName = null) where TEnum : struct, Enum
    Parameters
    Type Name Description
    TEnum? value

    The value to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which value corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Type Parameters
    Name Description
    TEnum

    The type of the enumeration.

    Remarks

    If the value is null, the validation is ignored (i.e. does not throws an ArgumentException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentException

    .

    | Edit this page View Source

    NotMatch(string?, string, RegexOptions, string, string?)

    Throws an ArgumentException if the regular expression specified in pattern not finds a match in parameter, with this error message: Parameter not matches the specified regular expression.

    Declaration
    public Guard NotMatch(string? parameter, string pattern, RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Compiled, string message = "", string? parameterName = null)
    Parameters
    Type Name Description
    string parameter

    The parameter to check.

    string pattern

    The regular expression pattern to match.

    RegexOptions options

    The regular expression options.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which parameter corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Remarks

    If the parameter is null, the validation is ignored (i.e. does not throws an ArgumentException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentException

    .

    | Edit this page View Source

    NotMatch<TException>(string?, string, RegexOptions, string, string?)

    Throws an ArgumentException if the regular expression specified in pattern not finds a match in parameter, with this error message: Parameter not matches the specified regular expression.

    Declaration
    public Guard NotMatch<TException>(string? parameter, string pattern, RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Compiled, string message = "", string? parameterName = null) where TException : Exception
    Parameters
    Type Name Description
    string parameter

    The parameter to check.

    string pattern

    The regular expression pattern to match.

    RegexOptions options

    The regular expression options.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which parameter corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Type Parameters
    Name Description
    TException

    The type of the exception.

    Remarks

    If the parameter is null, the validation is ignored (i.e. does not throws an TException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentException

    .

    | Edit this page View Source

    NullOrEmpty(string?, string, string?)

    Throws an ArgumentNullException if the parameter is null or an empty string, with this error message: Parameter cannot be null or empty.

    Declaration
    public Guard NullOrEmpty(string? parameter, string message = "", string? parameterName = null)
    Parameters
    Type Name Description
    string parameter

    The parameter to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which parameter corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Exceptions
    Type Condition
    ArgumentNullException

    parameter is null.

    | Edit this page View Source

    NullOrWhiteSpace(string?, string, string?)

    Throws an ArgumentNullException if the parameter is null, an empty string or consists only of white-space, with this error message: Parameter cannot be null, empty or consists exclusively of white-space characters.

    Declaration
    public Guard NullOrWhiteSpace(string? parameter, string message = "", string? parameterName = null)
    Parameters
    Type Name Description
    string parameter

    The parameter to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which parameter corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Exceptions
    Type Condition
    ArgumentNullException

    parameter is null.

    ArgumentException

    parameter is empty, or consists only of white-space.

    | Edit this page View Source

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

    Throws an ArgumentNullException if the parameter is null, with this error message: Parameter cannot be null.

    Declaration
    public Guard Null<T>(T? parameter, string message = "", string? parameterName = null)
    Parameters
    Type Name Description
    T parameter

    The parameter to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which parameter corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Type Parameters
    Name Description
    T

    The type of the parameter.

    Exceptions
    Type Condition
    ArgumentNullException

    parameter is null.

    | Edit this page View Source

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

    Throws an ArgumentException if the number is positive, with this error message: Number cannot be positive.

    Declaration
    public Guard Positive<T>(T? number, string message = "", string? parameterName = null) where T : struct, IComparable
    Parameters
    Type Name Description
    T? number

    The number to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which number corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Type Parameters
    Name Description
    T

    The type of the number.

    Remarks

    If the number is null, the validation is ignored (i.e. does not throws an ArgumentException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentException

    .

    | Edit this page View Source

    True(bool?, string, string?)

    Throws an ArgumentException if the parameter is true, with this error message: Parameter cannot be true.

    Declaration
    public Guard True(bool? parameter, string message = "", string? parameterName = null)
    Parameters
    Type Name Description
    bool? parameter

    The parameter to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which parameter corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Remarks

    If the parameter is null, the validation is ignored (i.e. does not throws an ArgumentException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentException

    .

    | Edit this page View Source

    True(Predicate<bool>?, string, string?)

    Throws an ArgumentException if the predicate is true, with this error message: Predicate cannot be true. Or, if the optional parameter message was set, with their message.

    Declaration
    public Guard True(Predicate<bool>? predicate, string message = "", string? parameterName = null)
    Parameters
    Type Name Description
    Predicate<bool> predicate

    The predicate to check.

    string message

    The optional error message passed to ArgumentException if the predicate is true.

    string parameterName

    The name of the parameter with which predicate corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Remarks

    If the predicate is null, the validation is ignored (i.e. does not throws an ArgumentException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentException

    .

    | Edit this page View Source

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

    Throws an ArgumentException if the number is zero or less than zero (i.e. negative), with this error message: 'The number cannot be zero.', if it is zero. Or 'The number cannot be negative.', if it is negative.

    This method is equivalent to chain the Zero(number).Negative(number) methods.

    Declaration
    public Guard ZeroOrLess<T>(T? number, string message = "", string? parameterName = null) where T : struct, IComparable
    Parameters
    Type Name Description
    T? number

    The number to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which number corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Type Parameters
    Name Description
    T

    The type of the number.

    Remarks

    If the number is null, the validation is ignored (i.e. does not throws an ArgumentException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentException

    .

    | Edit this page View Source

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

    Throws an ArgumentException if the number is zero, with this error message: Number cannot be zero.

    Declaration
    public Guard Zero<T>(T? number, string message = "", string? parameterName = null) where T : struct, IComparable
    Parameters
    Type Name Description
    T? number

    The number to check.

    string message

    The optional error message that explains the reason for the exception. If this parameter is provided, it will override the error message described in the summary section.

    string parameterName

    The name of the parameter with which number corresponds. If you omit this parameter, the name of parameter is used.

    Returns
    Type Description
    Guard

    The guard.

    Type Parameters
    Name Description
    T

    The type of the number.

    Remarks

    If the number is null, the validation is ignored (i.e. does not throws an ArgumentException). This is useful for optional parameters whose default value is null.

    Exceptions
    Type Condition
    ArgumentException

    .

    • Edit this page
    • View Source
    In this article
    Back to top © 2019–2026 Maicon Heck