Class Validate
The static Validate class contains a collection of methods for validating arguments; mainly to ensure they're not null or empty. The name of the value being validated must be passed in to ensure that it can be included in any exception message.
There are several practical advantages of these methods.
- The name of the parameter that failed validation will be included in the exception message.
- They're expressive, but succinct - they'll save you several lines of code every time you need a check.
- If the value validates, the method returns it - so pass-through validation is possible in places like chained constructor calls, where you just can't write several lines of code to do this.
Since the nameof operator was introduced in C# 6.0, it's been possible for callers of the methods in this class to use nameof to retrieve the value's name in a way that will not be broken by refactoring. When using older versions of C#, it was necessary to pass an explicit string value.
Inheritance
Inherited Members
Namespace: Existential
Assembly: Existential.Net.dll
Syntax
public static class Validate
Methods
View SourceThrowIf<T>(Predicate<T>, T, String, String, Object[])
inValue
is satisfies the predicate passed as inPredicate
.
Declaration
public static T ThrowIf<T>(Predicate<T> inPredicate, T inValue, string inName, string inMessage, params object[] inMessageArguments)
Parameters
Type | Name | Description |
---|---|---|
System.Predicate<T> | inPredicate | A predicate that must be satisfied by the value passed as |
T | inValue | A value that will be assessed by the predicate passed as |
System.String | inName | The name of the variable passed to the |
System.String | inMessage | A message to be included within the exception message, if it's thrown. |
System.Object[] | inMessageArguments | Additional values to be included in the message |
Returns
Type | Description |
---|---|
T | The value passed as parameter |
Type Parameters
Name | Description |
---|---|
T | The type parameter |
Exceptions
Type | Condition |
---|---|
System.ArgumentException |
An System.ArgumentException will be thrown if parameter
If the parameter |
ThrowIfEmptyGuid(Guid, String)
inValue
.
Declaration
public static Guid ThrowIfEmptyGuid(Guid inValue, string inName)
Parameters
Type | Name | Description |
---|---|---|
System.Guid | inValue | A value of type System.Guid to be checked for equality with the empty value System.Guid.Empty. The
name of the variable containing the value should be passed to this method as the
|
System.String | inName | The name of the variable passed to the |
Returns
Type | Description |
---|---|
System.Guid | The value passed as parameter |
Exceptions
Type | Condition |
---|---|
System.ArgumentException |
A System.ArgumentException will be thrown if the GUID
If the parameter |
ThrowIfEqualTo<T>(T, T, String)
inValue
is equal to the value passed as inForbiddenValue
.
There is no corresponding ThrowIfNotEqualTo method.
Declaration
public static T ThrowIfEqualTo<T>(T inForbiddenValue, T inValue, string inName)
where T : IComparable<T>
Parameters
Type | Name | Description |
---|---|---|
T | inForbiddenValue | A value which the value |
T | inValue | A value that will be validated to ensure that it is not equal to |
System.String | inName | The name of the variable passed to the |
Returns
Type | Description |
---|---|
T | The value passed as parameter |
Type Parameters
Name | Description |
---|---|
T | The type parameter |
Remarks
No corresponding ThrowIfNotEqualTo method has been provided, because it's a trivial case - it would be asking to throw an exception for every possible value except one! If that's really what's required it can certainly be handled better elsewhere in the code.
Exceptions
Type | Condition |
---|---|
System.ArgumentOutOfRangeException |
A System.ArgumentOutOfRangeException will be thrown if parameter
If the parameter |
ThrowIfGreaterThan<T>(T, T, String)
inValue
is greater than the value passed as inLimit
.
Declaration
public static T ThrowIfGreaterThan<T>(T inLimit, T inValue, string inName)
where T : IComparable<T>
Parameters
Type | Name | Description |
---|---|---|
T | inLimit | A value which the value |
T | inValue | A value that will be validated to ensure that it is less than or equal to |
System.String | inName | The name of the variable passed to the |
Returns
Type | Description |
---|---|
T | The value passed as parameter |
Type Parameters
Name | Description |
---|---|
T | The type parameter |
Exceptions
Type | Condition |
---|---|
System.ArgumentOutOfRangeException |
A System.ArgumentOutOfRangeException will be thrown if parameter
If the parameter |
ThrowIfGreaterThanOrEqualTo<T>(T, T, String)
inValue
is greater than or equal to the value passed as inLimit
.
Declaration
public static T ThrowIfGreaterThanOrEqualTo<T>(T inLimit, T inValue, string inName)
where T : IComparable<T>
Parameters
Type | Name | Description |
---|---|---|
T | inLimit | A value which the value |
T | inValue | A value that will be validated to ensure that it is less than |
System.String | inName | The name of the variable passed to the |
Returns
Type | Description |
---|---|
T | The value passed as parameter |
Type Parameters
Name | Description |
---|---|
T | The type parameter |
Exceptions
Type | Condition |
---|---|
System.ArgumentOutOfRangeException |
A System.ArgumentOutOfRangeException will be thrown if parameter
If the parameter |
ThrowIfLessThan<T>(T, T, String)
inValue
is less than the value passed as inLimit
.
Declaration
public static T ThrowIfLessThan<T>(T inLimit, T inValue, string inName)
where T : IComparable<T>
Parameters
Type | Name | Description |
---|---|---|
T | inLimit | A value which the value |
T | inValue | A value that will be validated to ensure that it is greater than or equal to |
System.String | inName | The name of the variable passed to the |
Returns
Type | Description |
---|---|
T | The value passed as parameter |
Type Parameters
Name | Description |
---|---|
T | The type parameter |
Exceptions
Type | Condition |
---|---|
System.ArgumentOutOfRangeException |
A System.ArgumentOutOfRangeException will be thrown if parameter
If the parameter |
ThrowIfLessThanOrEqualTo<T>(T, T, String)
inValue
is less than or equal to the value passed as inLimit
.
Declaration
public static T ThrowIfLessThanOrEqualTo<T>(T inLimit, T inValue, string inName)
where T : IComparable<T>
Parameters
Type | Name | Description |
---|---|---|
T | inLimit | A value which the value |
T | inValue | A value that will be validated to ensure that it is less than or equal to |
System.String | inName | The name of the variable passed to the |
Returns
Type | Description |
---|---|
T | The value passed as parameter |
Type Parameters
Name | Description |
---|---|
T | The type parameter |
Exceptions
Type | Condition |
---|---|
System.ArgumentOutOfRangeException |
A System.ArgumentOutOfRangeException will be thrown if parameter
If the parameter |
ThrowIfNot<T>(Predicate<T>, T, String, String, Object[])
inValue
does not satisfy the predicate passed as inPredicate
.
Declaration
public static T ThrowIfNot<T>(Predicate<T> inPredicate, T inValue, string inName, string inMessage, params object[] inMessageArguments)
Parameters
Type | Name | Description |
---|---|---|
System.Predicate<T> | inPredicate | A predicate that must be satisfied by the value passed as |
T | inValue | A value that will be assessed by the predicate passed as |
System.String | inName | The name of the variable passed to the |
System.String | inMessage | A message to be included within the exception message, if it's thrown. |
System.Object[] | inMessageArguments | Additional values to be included in the message |
Returns
Type | Description |
---|---|
T | The value passed as parameter |
Type Parameters
Name | Description |
---|---|
T | The type parameter |
Exceptions
Type | Condition |
---|---|
System.ArgumentException |
An System.ArgumentException will be thrown if parameter
If the parameter |
ThrowIfNotOfType(Type, Object, String)
inActual
isn't of either the type inType
or a
type derived from it.
Declaration
public static object ThrowIfNotOfType(Type inType, object inActual, string inName)
Parameters
Type | Name | Description |
---|---|---|
System.Type | inType | The type |
System.Object | inActual | An object that should be of type |
System.String | inName | The name of the variable passed to the |
Returns
Type | Description |
---|---|
System.Object | The object passed as parameter |
Remarks
If the type of inType
is known at compile time, then the generic version of
this method, ThrowIfNotOfType<T>(Object, String) may be preferred.
Exceptions
Type | Condition |
---|---|
ArgumentTypeException |
A ArgumentTypeException will be thrown if the object
If the parameter |
ThrowIfNotOfType<T>(Object, String)
inActual
isn't of either the type T
or a
type derived from it.
Declaration
public static T ThrowIfNotOfType<T>(object inActual, string inName)
where T : class
Parameters
Type | Name | Description |
---|---|---|
System.Object | inActual | An object that should be of type |
System.String | inName | The name of the variable passed to the |
Returns
Type | Description |
---|---|
T | The object passed as parameter |
Type Parameters
Name | Description |
---|---|
T | The type parameter |
Remarks
If a type such as T
is not known at compile time, then the more dynamic version of
this method, ThrowIfNotOfType(Type, Object, String) can be used instead.
Exceptions
Type | Condition |
---|---|
ArgumentTypeException |
A ArgumentTypeException will be thrown if the object
If the parameter |
ThrowIfNull<T>(T, String)
inValue
is null.
Declaration
public static T ThrowIfNull<T>(T inValue, string inName)
Parameters
Type | Name | Description |
---|---|---|
T | inValue | A value that will be validated to ensure that it's non-null. The name of the variable containing the value should
be passed to this method as the
|
System.String | inName | The name of the variable passed to the |
Returns
Type | Description |
---|---|
T | The value passed as parameter |
Type Parameters
Name | Description |
---|---|
T | The type parameter |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
A System.ArgumentNullException will be thrown if the object
If the parameter |
ThrowIfNullOrEmpty(String, String)
ThrowIfNullOrEmpty
will throw an System.ArgumentNullException if a null value is passed as the parameter
inValue
, or an System.ArgumentException if the string exists but is empty.
Declaration
public static string ThrowIfNullOrEmpty(string inValue, string inName)
Parameters
Type | Name | Description |
---|---|---|
System.String | inValue | A string that will be validated to ensure that it's neither null nor empty. The name of the variable containing
the string should be passed to this method as the
|
System.String | inName | The name of the variable passed to the |
Returns
Type | Description |
---|---|
System.String | The string passed as parameter |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
A System.ArgumentNullException will be thrown if the value of parameter
If the parameter |
System.ArgumentException |
A System.ArgumentException will be thrown if the string passed as parameter
If the parameter |
See Also
View SourceThrowIfNullOrEmpty(String, String, Boolean)
ThrowIfNullOrEmpty
will throw an System.ArgumentNullException if a null value is passed as the parameter
inValue
, or an System.ArgumentException if the string exists but is empty.
Declaration
public static string ThrowIfNullOrEmpty(string inValue, string inName, bool inTrim)
Parameters
Type | Name | Description |
---|---|---|
System.String | inValue | A string that will be validated to ensure that it's neither null nor empty. The name of the variable containing
the string should be passed to this method as the
|
System.String | inName | The name of the variable passed to the |
System.Boolean | inTrim | A Boolean value indicating whether or not white space should be trimmed from each end of the string. This can
result in a string that contains only white space characters being treated as empty. However the behaviour
of this method is not identical to ThrowIfNullOrWhiteSpace(String, String). This method can change the value of the
string passed as |
Returns
Type | Description |
---|---|
System.String | The string passed as parameter |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
A System.ArgumentNullException will be thrown if the value of parameter
If the parameter |
System.ArgumentException |
A System.ArgumentException will be thrown if the string passed as parameter
If the parameter |
See Also
View SourceThrowIfNullOrEmpty<T>(T, String)
inCollection
, or an System.ArgumentException if the collection exists but is empty.
Declaration
public static T ThrowIfNullOrEmpty<T>(T inCollection, string inName)
where T : class, IEnumerable
Parameters
Type | Name | Description |
---|---|---|
T | inCollection | A collection that will be validated to ensure that it's neither null nor empty. The name of the variable containing
the collection should be passed to this method as the
|
System.String | inName | The name of the variable passed to the |
Returns
Type | Description |
---|---|
T | The collection passed as parameter |
Type Parameters
Name | Description |
---|---|
T | The type parameter |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
A System.ArgumentNullException will be thrown if the value of parameter
If the parameter |
System.ArgumentException |
A System.ArgumentException will be thrown if the collection passed as parameter
If the parameter |
ThrowIfNullOrWhiteSpace(String, String)
inValue
, or an System.ArgumentException if the string exists but is either empty or
contains only white space characters.
Declaration
public static string ThrowIfNullOrWhiteSpace(string inValue, string inName)
Parameters
Type | Name | Description |
---|---|---|
System.String | inValue | A string that will be validated to ensure that it's neither null nor empty, nor contains only white space
characters. The name of the variable containing the string should be passed to this method as the
|
System.String | inName | The name of the variable passed to the |
Returns
Type | Description |
---|---|
System.String | The string passed as parameter |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
A System.ArgumentNullException will be thrown if the value of parameter
If the parameter |
System.ArgumentException |
A System.ArgumentException will be thrown if the string passed as parameter
If the parameter |