Show / Hide Table of Contents

    Class Check

    Helper class for validating method parameters. Every validation throws some kind of System.ArgumentException if it fails (System.ArgumentException, System.ArgumentNullException, System.ArgumentOutOfRangeException).

    Inheritance
    System.Object
    Check
    Namespace: Kros.Utils
    Assembly: Kros.Utils.dll
    Syntax
    public static class Check
    Remarks

    Default way of validating method parameters is:

        private string _value1;
        private int _value2;
    
        public void MethodWithParameters(string arg1, int arg2)
        {
            if (string.IsNullOrEmpty(arg1))
            {
                throw new ArgumentNullException(nameof(arg1));
            }
            if (arg2 <= 0)
            {
                throw new ArgumentException("Hodnota parametra arg2 musí byť väčšia ako 0.", nameof(arg2));
            }
    
            _value1 = arg1;
            _value2 = arg2;
    
            // ...
        }
    With the help of <code>Check</code> class, this is very easy. If it is possible, the validation methods return input value,
    so the parameter&apos;s value can be validated and assigned on one line:
    
        private string _value1;
        private int _value2;
    
        public void MethodWithParameters(string arg1, int arg2)
        {
            _value1 = Check.NotNullOrEmpty(arg1, nameof(arg1));
            _value2 = Check.GreaterThan(arg2, 0, nameof(arg2));
    
            // ...
        }

    Methods

    Equal<T>(T, T, String)

    The value of param must be value.

    Declaration
    public static T Equal<T>(T param, T value, string paramName)
    Parameters
    Type Name Description
    T param

    Validated value.

    T value

    Required value of the param.

    System.String paramName

    Name of the method parameter.

    Returns
    Type Description
    T

    Input value of param.

    Type Parameters
    Name Description
    T

    Type of the param.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is not value.

    Equal<T>(T, T, String, String)

    The value of param must be value. Thrown exception has custom message message.

    Declaration
    public static T Equal<T>(T param, T value, string paramName, string message)
    Parameters
    Type Name Description
    T param

    Validated value.

    T value

    Required value of the param.

    System.String paramName

    Name of the method parameter.

    System.String message

    Custom exception message.

    Returns
    Type Description
    T

    Input value of param.

    Type Parameters
    Name Description
    T

    Type of the param.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is not value.

    GreaterOrEqualThan<T>(T, T, String)

    The value of param must be greater or equal than value.

    Declaration
    public static T GreaterOrEqualThan<T>(T param, T value, string paramName)
    
        where T : IComparable<T>
    Parameters
    Type Name Description
    T param

    Validated value.

    T value

    Checked value of param.

    System.String paramName

    Name of the method parameter.

    Returns
    Type Description
    T

    Input value of param.

    Type Parameters
    Name Description
    T

    Type of the param.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is less than value.

    GreaterOrEqualThan<T>(T, T, String, String)

    The value of param must be greater or equal than value. Thrown exception has custom message message.

    Declaration
    public static T GreaterOrEqualThan<T>(T param, T value, string paramName, string message)
    
        where T : IComparable<T>
    Parameters
    Type Name Description
    T param

    Validated value.

    T value

    Checked value of param.

    System.String paramName

    Name of the method parameter.

    System.String message

    Custom exception message.

    Returns
    Type Description
    T

    Input value of param.

    Type Parameters
    Name Description
    T

    Type of the param.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is less than value.

    GreaterThan<T>(T, T, String)

    The value of param must be greater than value.

    Declaration
    public static T GreaterThan<T>(T param, T value, string paramName)
    
        where T : IComparable<T>
    Parameters
    Type Name Description
    T param

    Validated value.

    T value

    Checked value of param.

    System.String paramName

    Name of the method parameter.

    Returns
    Type Description
    T

    Input value of param.

    Type Parameters
    Name Description
    T

    Type of the param.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is less or equal than value.

    GreaterThan<T>(T, T, String, String)

    The value of param must be greater than value. Thrown exception has custom message message.

    Declaration
    public static T GreaterThan<T>(T param, T value, string paramName, string message)
    
        where T : IComparable<T>
    Parameters
    Type Name Description
    T param

    Validated value.

    T value

    Checked value of param.

    System.String paramName

    Name of the method parameter.

    System.String message

    Custom exception message.

    Returns
    Type Description
    T

    Input value of param.

    Type Parameters
    Name Description
    T

    Type of the param.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is less or equal than value.

    IsInList<T>(T, IEnumerable<T>, String)

    The value of param must be in list list.

    Declaration
    public static T IsInList<T>(T param, IEnumerable<T> list, string paramName)
    Parameters
    Type Name Description
    T param

    The value, which must be in the list list.

    System.Collections.Generic.IEnumerable<T> list

    List of checked values.

    System.String paramName

    Name of the method parameter.

    Returns
    Type Description
    T

    Input value of param.

    Type Parameters
    Name Description
    T

    Type of the param.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is not in the list list.

    IsInList<T>(T, IEnumerable<T>, String, String)

    The value of param must be in list list. Thrown exception has custom message message.

    Declaration
    public static T IsInList<T>(T param, IEnumerable<T> list, string paramName, string message)
    Parameters
    Type Name Description
    T param

    The value, which must be in the list list.

    System.Collections.Generic.IEnumerable<T> list

    List of checked values.

    System.String paramName

    Name of the method parameter.

    System.String message

    Custom exception message.

    Returns
    Type Description
    T

    Input value of param.

    Type Parameters
    Name Description
    T

    Type of the param.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is not in the list list.

    IsNotInList<T>(T, IEnumerable<T>, String)

    The value of param must not be in list list.

    Declaration
    public static T IsNotInList<T>(T param, IEnumerable<T> list, string paramName)
    Parameters
    Type Name Description
    T param

    The value, which must not be in the list list.

    System.Collections.Generic.IEnumerable<T> list

    List of checked values.

    System.String paramName

    Name of the method parameter.

    Returns
    Type Description
    T

    Input value of param.

    Type Parameters
    Name Description
    T

    Type of the param.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is in the list list.

    IsNotInList<T>(T, IEnumerable<T>, String, String)

    The value of param must not be in list list. Thrown exception has custom message message.

    Declaration
    public static T IsNotInList<T>(T param, IEnumerable<T> list, string paramName, string message)
    Parameters
    Type Name Description
    T param

    The value, which must not be in the list list.

    System.Collections.Generic.IEnumerable<T> list

    List of checked values.

    System.String paramName

    Name of the method parameter.

    System.String message

    Custom exception message.

    Returns
    Type Description
    T

    Input value of param.

    Type Parameters
    Name Description
    T

    Type of the param.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is in the list list.

    IsNotOfType(Object, Type, String)

    The value of param must not be of type notExpectedType.

    Declaration
    public static void IsNotOfType(object param, Type notExpectedType, string paramName)
    Parameters
    Type Name Description
    System.Object param

    Validated value.

    System.Type notExpectedType

    The value of param must not be of this type.

    System.String paramName

    Name of the method parameter.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is of forbidden type.

    IsNotOfType(Object, Type, String, String)

    The value of param must not be of type notExpectedType. Thrown exception has custom message message.

    Declaration
    public static void IsNotOfType(object param, Type notExpectedType, string paramName, string message)
    Parameters
    Type Name Description
    System.Object param

    Validated value.

    System.Type notExpectedType

    The value of param must not be of this type.

    System.String paramName

    Name of the method parameter.

    System.String message

    Custom exception message.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is of forbidden type.

    IsNotOfType<T>(Object, String)

    The value of param must not be of given type T.

    Declaration
    public static void IsNotOfType<T>(object param, string paramName)
    Parameters
    Type Name Description
    System.Object param

    Validated value.

    System.String paramName

    Name of the method parameter.

    Type Parameters
    Name Description
    T

    The value of param must not be of this type.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is of forbidden type.

    IsNotOfType<T>(Object, String, String)

    The value of param must not be of given type T. Thrown exception has custom message message.

    Declaration
    public static void IsNotOfType<T>(object param, string paramName, string message)
    Parameters
    Type Name Description
    System.Object param

    Validated value.

    System.String paramName

    Name of the method parameter.

    System.String message

    Custom exception message.

    Type Parameters
    Name Description
    T

    The value of param must not be of this type.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is of forbidden type.

    IsOfType(Object, Type, String)

    The value of param must be of given type expectedType.

    Declaration
    public static void IsOfType(object param, Type expectedType, string paramName)
    Parameters
    Type Name Description
    System.Object param

    Validated value.

    System.Type expectedType

    Required type of param.

    System.String paramName

    Name of the method parameter.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is not of required type.

    IsOfType(Object, Type, String, String)

    The value of param must be of given type expectedType. Thrown exception has custom message message.

    Declaration
    public static void IsOfType(object param, Type expectedType, string paramName, string message)
    Parameters
    Type Name Description
    System.Object param

    Validated value.

    System.Type expectedType

    Required type of param.

    System.String paramName

    Name of the method parameter.

    System.String message

    Custom exception message.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is not of required type.

    IsOfType<T>(Object, String)

    The value of param must be of given type T.

    Declaration
    public static void IsOfType<T>(object param, string paramName)
    Parameters
    Type Name Description
    System.Object param

    Validated value.

    System.String paramName

    Name of the method parameter.

    Type Parameters
    Name Description
    T

    Expected type of param.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is not of required type.

    IsOfType<T>(Object, String, String)

    The value of param must be of given type T. Thrown exception has custom message message.

    Declaration
    public static void IsOfType<T>(object param, string paramName, string message)
    Parameters
    Type Name Description
    System.Object param

    Validated value.

    System.String paramName

    Name of the method parameter.

    System.String message

    Custom exception message.

    Type Parameters
    Name Description
    T

    Expected type of param.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is not of required type.

    LessOrEqualThan<T>(T, T, String)

    The value of param must be less or equal than value.

    Declaration
    public static T LessOrEqualThan<T>(T param, T value, string paramName)
    
        where T : IComparable<T>
    Parameters
    Type Name Description
    T param

    Validated value.

    T value

    Checked value of param.

    System.String paramName

    Name of the method parameter.

    Returns
    Type Description
    T

    Input value of param.

    Type Parameters
    Name Description
    T

    Type of the param.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is greater than value.

    LessOrEqualThan<T>(T, T, String, String)

    The value of param must be less or equal than value. Thrown exception has custom message message.

    Declaration
    public static T LessOrEqualThan<T>(T param, T value, string paramName, string message)
    
        where T : IComparable<T>
    Parameters
    Type Name Description
    T param

    Validated value.

    T value

    Checked value of param.

    System.String paramName

    Name of the method parameter.

    System.String message

    Custom exception message.

    Returns
    Type Description
    T

    Input value of param.

    Type Parameters
    Name Description
    T

    Type of the param.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is greater than value.

    LessThan<T>(T, T, String)

    The value of param must be less than value.

    Declaration
    public static T LessThan<T>(T param, T value, string paramName)
    
        where T : IComparable<T>
    Parameters
    Type Name Description
    T param

    Validated value.

    T value

    Checked value of param.

    System.String paramName

    Name of the method parameter.

    Returns
    Type Description
    T

    Input value of param.

    Type Parameters
    Name Description
    T

    Type of the param.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is greater or equal than value.

    LessThan<T>(T, T, String, String)

    The value of param must be less than value. Thrown exception has custom message message.

    Declaration
    public static T LessThan<T>(T param, T value, string paramName, string message)
    
        where T : IComparable<T>
    Parameters
    Type Name Description
    T param

    Validated value.

    T value

    Checked value of param.

    System.String paramName

    Name of the method parameter.

    System.String message

    Custom exception message.

    Returns
    Type Description
    T

    Input value of param.

    Type Parameters
    Name Description
    T

    Type of the param.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is greater or equal than value.

    NotEmptyGuid(Guid, String)

    The value of param can not be empty GUID (System.Guid.Empty).

    Declaration
    public static Guid NotEmptyGuid(Guid param, string paramName)
    Parameters
    Type Name Description
    System.Guid param

    Validated value.

    System.String paramName

    Name of the method parameter.

    Returns
    Type Description
    System.Guid

    Input value of param.

    Exceptions
    Type Condition
    System.ArgumentException

    Paramere je prázdny GUID (System.Guid.Empty).

    NotEmptyGuid(Guid, String, String)

    The value of param can not be empty GUID (System.Guid.Empty). Thrown exception has custom message message.

    Declaration
    public static Guid NotEmptyGuid(Guid param, string paramName, string message)
    Parameters
    Type Name Description
    System.Guid param

    Validated value.

    System.String paramName

    Name of the method parameter.

    System.String message

    Custom exception message.

    Returns
    Type Description
    System.Guid

    Input value of param.

    Exceptions
    Type Condition
    System.ArgumentException

    Value of param is empty GUID (System.Guid.Empty).

    NotEqual<T>(T, T, String)

    The value of param must not be value.

    Declaration
    public static T NotEqual<T>(T param, T value, string paramName)
    Parameters
    Type Name Description
    T param

    Validated value.

    T value

    Checked value of param.

    System.String paramName

    Name of the method parameter.

    Returns
    Type Description
    T

    Input value of param.

    Type Parameters
    Name Description
    T

    Type of the param.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is value.

    NotEqual<T>(T, T, String, String)

    The value of param must not be value. Thrown exception has custom message message.

    Declaration
    public static T NotEqual<T>(T param, T value, string paramName, string message)
    Parameters
    Type Name Description
    T param

    Validated value.

    T value

    Checked value of param.

    System.String paramName

    Name of the method parameter.

    System.String message

    Custom exception message.

    Returns
    Type Description
    T

    Input value of param.

    Type Parameters
    Name Description
    T

    Type of the param.

    Exceptions
    Type Condition
    System.ArgumentException

    The value of param is value.

    NotNull<T>(T, String)

    The value of param can not be null.

    Declaration
    public static T NotNull<T>(T param, string paramName)
    Parameters
    Type Name Description
    T param

    Validated value.

    System.String paramName

    Name of the method parameter.

    Returns
    Type Description
    T

    Input value of param.

    Type Parameters
    Name Description
    T

    Type of the param.

    Exceptions
    Type Condition
    System.ArgumentNullException

    The value of param is null.

    NotNull<T>(T, String, String)

    The value of param can not be null. Thrown exception has custom message message.

    Declaration
    public static T NotNull<T>(T param, string paramName, string message)
    Parameters
    Type Name Description
    T param

    Validated value.

    System.String paramName

    Name of the method parameter.

    System.String message

    Custom exception message.

    Returns
    Type Description
    T

    Input value of param.

    Type Parameters
    Name Description
    T

    Type of the param.

    Exceptions
    Type Condition
    System.ArgumentNullException

    The value of param is null.

    NotNullOrEmpty(String, String)

    The value of param can not be null, nor empty string.

    Declaration
    public static string NotNullOrEmpty(string param, string paramName)
    Parameters
    Type Name Description
    System.String param

    Validated value.

    System.String paramName

    Name of the method parameter.

    Returns
    Type Description
    System.String

    Input value of param.

    Exceptions
    Type Condition
    System.ArgumentNullException

    The value of param is null.

    System.ArgumentException

    The value of param is empty string.

    NotNullOrEmpty(String, String, String)

    The value of param can not be null, nor empty string. Thrown exception has custom message message.

    Declaration
    public static string NotNullOrEmpty(string param, string paramName, string message)
    Parameters
    Type Name Description
    System.String param

    Validated value.

    System.String paramName

    Name of the method parameter.

    System.String message

    Custom exception message.

    Returns
    Type Description
    System.String

    Input value of param.

    Exceptions
    Type Condition
    System.ArgumentNullException

    The value of param is null.

    System.ArgumentException

    The value of param is empty string.

    NotNullOrWhiteSpace(String, String)

    The value of param can not be null, empty string, nor string containing only whitespace characters.

    Declaration
    public static string NotNullOrWhiteSpace(string param, string paramName)
    Parameters
    Type Name Description
    System.String param

    Validated value.

    System.String paramName

    Name of the method parameter.

    Returns
    Type Description
    System.String

    Input value of param.

    Exceptions
    Type Condition
    System.ArgumentNullException

    The value of param is null.

    System.ArgumentException

    The value of param is empty string or string containing only whitespace characters.

    NotNullOrWhiteSpace(String, String, String)

    The value of param can not be null, empty string, nor string containing only whitespace characters. Thrown exception has custom message message.

    Declaration
    public static string NotNullOrWhiteSpace(string param, string paramName, string message)
    Parameters
    Type Name Description
    System.String param

    Validated value.

    System.String paramName

    Name of the method parameter.

    System.String message

    Custom exception message.

    Returns
    Type Description
    System.String

    Input value of param.

    Exceptions
    Type Condition
    System.ArgumentNullException

    The value of param is null.

    System.ArgumentException

    The value of param is empty string or string containing only whitespace characters.

    Back to top KROS a.s.