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
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'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 |
| System.String | paramName | Name of the method parameter. |
Returns
| Type | Description |
|---|---|
| T | Input value of |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
| System.String | paramName | Name of the method parameter. |
| System.String | message | Custom exception message. |
Returns
| Type | Description |
|---|---|
| T | Input value of |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
| System.String | paramName | Name of the method parameter. |
Returns
| Type | Description |
|---|---|
| T | Input value of |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
| System.String | paramName | Name of the method parameter. |
| System.String | message | Custom exception message. |
Returns
| Type | Description |
|---|---|
| T | Input value of |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
| System.String | paramName | Name of the method parameter. |
Returns
| Type | Description |
|---|---|
| T | Input value of |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
| System.String | paramName | Name of the method parameter. |
| System.String | message | Custom exception message. |
Returns
| Type | Description |
|---|---|
| T | Input value of |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
| 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 |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
| 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 |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
| 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 |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
| 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 |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
| System.String | paramName | Name of the method parameter. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
| System.String | paramName | Name of the method parameter. |
| System.String | message | Custom exception message. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
| System.String | paramName | Name of the method parameter. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
| System.String | paramName | Name of the method parameter. |
| System.String | message | Custom exception message. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
| System.String | paramName | Name of the method parameter. |
Returns
| Type | Description |
|---|---|
| T | Input value of |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
| System.String | paramName | Name of the method parameter. |
| System.String | message | Custom exception message. |
Returns
| Type | Description |
|---|---|
| T | Input value of |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
| System.String | paramName | Name of the method parameter. |
Returns
| Type | Description |
|---|---|
| T | Input value of |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
| System.String | paramName | Name of the method parameter. |
| System.String | message | Custom exception message. |
Returns
| Type | Description |
|---|---|
| T | Input value of |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
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 |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | Value of |
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 |
| System.String | paramName | Name of the method parameter. |
Returns
| Type | Description |
|---|---|
| T | Input value of |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
| System.String | paramName | Name of the method parameter. |
| System.String | message | Custom exception message. |
Returns
| Type | Description |
|---|---|
| T | Input value of |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | The value of |
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 |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | The value of |
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 |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | The value of |
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 |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | The value of |
| System.ArgumentException | The value of |
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 |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | The value of |
| System.ArgumentException | The value of |
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 |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | The value of |
| System.ArgumentException | The value of |
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 |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | The value of |
| System.ArgumentException | The value of |