Class Validate
- Namespace
- Existential
- Assembly
- Existential.Net.dll
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 parameter's name in a way that will not be broken by refactoring tools. When using older versions of C#, it was necessary to pass an explicit string value.
public static class Validate
- Inheritance
-
System.ObjectValidate
- Inherited Members
-
System.Object.Equals(System.Object)System.Object.Equals(System.Object, System.Object)System.Object.GetHashCode()System.Object.GetType()System.Object.MemberwiseClone()System.Object.ReferenceEquals(System.Object, System.Object)System.Object.ToString()
Methods
ThrowIf<T>(Predicate<T>, T, String, String, Object[])
inValue
is satisfies the predicate passed as inPredicate
.
public static T ThrowIf<T>(Predicate<T> inPredicate, T inValue, string inName, string inMessage, params object[] inMessageArguments)
Parameters
inPredicate
System.Predicate<T>A predicate that must be satisfied by the value passed as
inValue
if an exception is to be thrown.inValue
TA value that will be assessed by the predicate passed as
inPredicate
. It will be returned if the predicate is not satisfied. The name of the variable containing the value must be passed to this method as theinName
parameter.inName
System.StringThe name of the variable passed to the
inValue
parameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.inMessage
System.StringA message to be included within the exception message, if it's thrown.
inMessageArguments
System.Object[]Additional values to be included in the message
inMessage
.
Returns
- T
The value passed as parameter
inValue
will be returned if the predicate passed as valueinPredicate
is not satisfied.
Type Parameters
T
The type parameter
T
captures the type of the value passed to the parameterinValue
so that the value can be returned as exactly the same type if it validates successfully. The type should not have to be specified explicitly when calling the method. There are few constraints on the typeT
.
Exceptions
- System.ArgumentException
An System.ArgumentException will be thrown if parameter
inValue
satisfies the predicate passed asinPredicate
.If the parameter
inName
has been correctly populated, the name of the value will be included in the exception message.
ThrowIfEmptyGuid(Guid, String)
inValue
.
public static Guid ThrowIfEmptyGuid(Guid inValue, string inName)
Parameters
inValue
System.GuidA 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
inName
parameter.inName
System.StringThe name of the variable passed to the
inValue
parameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
Returns
- System.Guid
The value passed as parameter
inValue
will be returned if it's not the empty Guid.
Exceptions
- System.ArgumentException
A System.ArgumentException will be thrown if the GUID
inValue
has the value System.Guid.Empty.If the parameter
inName
has been correctly populated, the name of the value that was unexpectedly found to be empty will be included in the exception message.
ThrowIfEqualTo<T>(T, T, String)
inValue
is equal to the value passed as inForbiddenValue
.
There is no corresponding ThrowIfNotEqualTo method.
public static T ThrowIfEqualTo<T>(T inForbiddenValue, T inValue, string inName)
where T : IComparable<T>
Parameters
inForbiddenValue
TA value which the value
inValue
cannot be equal to. IfinValue
is equal toinForbiddenValue
, an System.ArgumentOutOfRangeException will be thrown.inValue
TA value that will be validated to ensure that it is not equal to
inForbiddenValue
. The name of the variable containing the value should be passed to this method as theinName
parameter.inName
System.StringThe name of the variable passed to the
inValue
parameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
Returns
- T
The value passed as parameter
inValue
will be returned if it validates as not being equal toinForbiddenValue
.
Type Parameters
T
The type parameter
T
captures the type of the value passed to the parameterinValue
so that the value can be returned as exactly the same type if it validates successfully. The type should not have to be specified explicitly when calling the method. There are few constraints on the typeT
; it must implement the interface System.IComparable`1.
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
- System.ArgumentOutOfRangeException
A System.ArgumentOutOfRangeException will be thrown if parameter
inValue
is equal to the parameterinForbiddenValue
.If the parameter
inName
has been correctly populated, the name of the value that was found to be greater thaninForbiddenValue
will be included in the exception message.
ThrowIfGreaterThan<T>(T, T, String)
inValue
is greater than the value passed as inLimit
.
public static T ThrowIfGreaterThan<T>(T inLimit, T inValue, string inName)
where T : IComparable<T>
Parameters
inLimit
TA value which the value
inValue
will be compared against as a limit that cannot be exceeded. IfinValue
is greater thaninLimit
, an System.ArgumentOutOfRangeException will be thrown.inValue
TA value that will be validated to ensure that it is less than or equal to
inLimit
. The name of the variable containing the value should be passed to this method as theinName
parameter.inName
System.StringThe name of the variable passed to the
inValue
parameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
Returns
- T
The value passed as parameter
inValue
will be returned if it validates as being less than or equal toinLimit
.
Type Parameters
T
The type parameter
T
captures the type of the value passed to the parameterinValue
so that the value can be returned as exactly the same type if it validates successfully. The type should not have to be specified explicitly when calling the method. There are few constraints on the typeT
; it must implement the interface System.IComparable`1.
Exceptions
- System.ArgumentOutOfRangeException
A System.ArgumentOutOfRangeException will be thrown if parameter
inValue
is greater than the parameterinLimit
.If the parameter
inName
has been correctly populated, the name of the value that was found to be greater thaninLimit
will be included in the exception message.
ThrowIfGreaterThanOrEqualTo<T>(T, T, String)
inValue
is greater than or equal to the value passed as inLimit
.
public static T ThrowIfGreaterThanOrEqualTo<T>(T inLimit, T inValue, string inName)
where T : IComparable<T>
Parameters
inLimit
TA value which the value
inValue
will be compared against as a limit that cannot be equalled or exceeded. IfinValue
is greater than or equal toinLimit
, an System.ArgumentOutOfRangeException will be thrown.inValue
TA value that will be validated to ensure that it is less than
inLimit
. The name of the variable containing the value should be passed to this method as theinName
parameter.inName
System.StringThe name of the variable passed to the
inValue
parameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
Returns
- T
The value passed as parameter
inValue
will be returned if it validates as being less thaninLimit
.
Type Parameters
T
The type parameter
T
captures the type of the value passed to the parameterinValue
so that the value can be returned as exactly the same type if it validates successfully. The type should not have to be specified explicitly when calling the method. There are few constraints on the typeT
; it must implement the interface System.IComparable`1.
Exceptions
- System.ArgumentOutOfRangeException
A System.ArgumentOutOfRangeException will be thrown if parameter
inValue
is greater than or equal to the parameterinLimit
.If the parameter
inName
has been correctly populated, the name of the value that was found to be greater thaninLimit
will be included in the exception message.
ThrowIfLessThan<T>(T, T, String)
inValue
is less than the value passed as inLimit
.
public static T ThrowIfLessThan<T>(T inLimit, T inValue, string inName)
where T : IComparable<T>
Parameters
inLimit
TA value which the value
inValue
will be compared against, as a lower limit that must be equalled or exceeded. IfinValue
is less thaninLimit
, an System.ArgumentOutOfRangeException will be thrown.inValue
TA value that will be validated to ensure that it is greater than or equal to
inLimit
. The name of the variable containing the value should be passed to this method as theinName
parameter.inName
System.StringThe name of the variable passed to the
inValue
parameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
Returns
- T
The value passed as parameter
inValue
will be returned if it validates as being less thaninLimit
.
Type Parameters
T
The type parameter
T
captures the type of the value passed to the parameterinValue
so that the value can be returned as exactly the same type if it validates successfully. The type should not have to be specified explicitly when calling the method. There are few constraints on the typeT
; it must implement the interface System.IComparable`1.
Exceptions
- System.ArgumentOutOfRangeException
A System.ArgumentOutOfRangeException will be thrown if parameter
inValue
is less than the parameterinLimit
.If the parameter
inName
has been correctly populated, the name of the value that was found to be less thaninLimit
will be included in the exception message.
ThrowIfLessThanOrEqualTo<T>(T, T, String)
inValue
is less than or equal to the value passed as inLimit
.
public static T ThrowIfLessThanOrEqualTo<T>(T inLimit, T inValue, string inName)
where T : IComparable<T>
Parameters
inLimit
TA value which the value
inValue
will be compared against, as a lower limit that must be exceeded. IfinValue
is less than or equal toinLimit
, an System.ArgumentOutOfRangeException will be thrown.inValue
TA value that will be validated to ensure that it is less than or equal to
inLimit
. The name of the variable containing the value should be passed to this method as theinName
parameter.inName
System.StringThe name of the variable passed to the
inValue
parameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
Returns
- T
The value passed as parameter
inValue
will be returned if it validates as being less than or equal toinLimit
.
Type Parameters
T
The type parameter
T
captures the type of the value passed to the parameterinValue
so that the value can be returned as exactly the same type if it validates successfully. The type should not have to be specified explicitly when calling the method. There are few constraints on the typeT
; it must implement the interface System.IComparable`1.
Exceptions
- System.ArgumentOutOfRangeException
A System.ArgumentOutOfRangeException will be thrown if parameter
inValue
is less than or equal to the parameterinLimit
.If the parameter
inName
has been correctly populated, the name of the value that was found to be less thaninLimit
will be included in the exception message.
ThrowIfNot<T>(Predicate<T>, T, String, String, Object[])
inValue
does not satisfy the predicate passed as inPredicate
.
public static T ThrowIfNot<T>(Predicate<T> inPredicate, T inValue, string inName, string inMessage, params object[] inMessageArguments)
Parameters
inPredicate
System.Predicate<T>A predicate that must be satisfied by the value passed as
inValue
if an exception is not to be thrown.inValue
TA value that will be assessed by the predicate passed as
inPredicate
. It will be returned if the predicate is satisfied. The name of the variable containing the value must be passed to this method as theinName
parameter.inName
System.StringThe name of the variable passed to the
inValue
parameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.inMessage
System.StringA message to be included within the exception message, if it's thrown.
inMessageArguments
System.Object[]Additional values to be included in the message
inMessage
.
Returns
- T
The value passed as parameter
inValue
will be returned if the predicate passed as valueinPredicate
is satisfied.
Type Parameters
T
The type parameter
T
captures the type of the value passed to the parameterinValue
so that the value can be returned as exactly the same type if it validates successfully. The type should not have to be specified explicitly when calling the method. There are few constraints on the typeT
.
Exceptions
- System.ArgumentException
An System.ArgumentException will be thrown if parameter
inValue
does not satisfy the predicate passed asinPredicate
.If the parameter
inName
has been correctly populated, the name of the value will be included in the exception message.
ThrowIfNotOfType(Type, Object, String)
inActual
isn't of either the type inType
or a
type derived from it.
public static object ThrowIfNotOfType(Type inType, object inActual, string inName)
Parameters
inType
System.TypeThe type
inType
specifies that theinActual
should be of either that type or one derived from it.inActual
System.ObjectAn object that should be of type
inType
or a type derived from it. The name of the variable containing the object should be passed to this method as theinName
parameter.inName
System.StringThe name of the variable passed to the
inActual
parameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
Returns
- System.Object
The object passed as parameter
inActual
will be returned if it's of the expected type or a derived one.
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
- ArgumentTypeException
A ArgumentTypeException will be thrown if the object
inActual
is neither of the typeinType
nor of a type derived from it.If the parameter
inName
has been correctly populated, the name of the value that was found to be of an unexpected type will be included in the exception message.
ThrowIfNotOfType<T>(Object, String)
inActual
isn't of either the type T
or a
type derived from it.
public static T ThrowIfNotOfType<T>(object inActual, string inName)
where T : class
Parameters
inActual
System.ObjectAn object that should be of type
T
or a type derived from it. The name of the variable containing the object should be passed to this method as theinName
parameter.inName
System.StringThe name of the variable passed to the
inActual
parameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
Returns
- T
The object passed as parameter
inActual
will be returned if it's of the expected type or a derived one.
Type Parameters
T
The type parameter
T
specifies that theinActual
should be of either that type or one derived from it.
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
- ArgumentTypeException
A ArgumentTypeException will be thrown if the object
inActual
is neither of the typeT
nor of a type derived from it.If the parameter
inName
has been correctly populated, the name of the value that was found to be of an unexpected type will be included in the exception message.
ThrowIfNull<T>(T, String)
inValue
is null.
public static T ThrowIfNull<T>(T inValue, string inName)
Parameters
inValue
TA 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
inName
parameter.inName
System.StringThe name of the variable passed to the
inValue
parameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
Returns
- T
The value passed as parameter
inValue
will be returned if it validates as non-null.
Type Parameters
T
The type parameter
T
captures the type of the parameterinValue
so that the value can be returned as the same type if it validates as not being null. The type should not have to be specified explicitly when calling the method.
Exceptions
- System.ArgumentNullException
A System.ArgumentNullException will be thrown if the object
inValue
is null.If the parameter
inName
has been correctly populated, the name of the value that was found to be unexpectedly null will be included in the exception message.
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.
public static string ThrowIfNullOrEmpty(string inValue, string inName)
Parameters
inValue
System.StringA 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
inName
parameter.inName
System.StringThe name of the variable passed to the
inValue
parameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
Returns
- System.String
The string passed as parameter
inValue
will be returned if it validates as being neither null nor empty.
Exceptions
- System.ArgumentNullException
A System.ArgumentNullException will be thrown if the value of parameter
inValue
is null.If the parameter
inName
has been correctly populated, the name of the string that was found to be unexpectedly null will be included in the exception message.- System.ArgumentException
A System.ArgumentException will be thrown if the string passed as parameter
inValue
is empty.If the parameter
inName
has been correctly populated, the name of the string that was found to be unexpectedly empty will be included in the exception message.
- See Also
ThrowIfNullOrEmpty(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.
public static string ThrowIfNullOrEmpty(string inValue, string inName, bool inTrim)
Parameters
inValue
System.StringA 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
inName
parameter.inName
System.StringThe name of the variable passed to the
inValue
parameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.inTrim
System.BooleanA Boolean value indicating whether 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
inValue
before returning it, while ThrowIfNullOrWhiteSpace(String, String) has no side effects.
Returns
- System.String
The string passed as parameter
inValue
will be returned if it validates as being neither null nor empty. If the value ofinTrim
wastrue
, then white space characters will be trimmed from either end of the string before it is returned.
Exceptions
- System.ArgumentNullException
A System.ArgumentNullException will be thrown if the value of parameter
inValue
is null.If the parameter
inName
has been correctly populated, the name of the string that was found to be unexpectedly null will be included in the exception message.- System.ArgumentException
A System.ArgumentException will be thrown if the string passed as parameter
inValue
is empty.If the parameter
inName
has been correctly populated, the name of the string that was found to be unexpectedly empty will be included in the exception message.
- See Also
ThrowIfNullOrEmpty<T>(T, String)
inCollection
, or an System.ArgumentException if the collection exists but is empty.
public static T ThrowIfNullOrEmpty<T>(T inCollection, string inName)
where T : class, IEnumerable
Parameters
inCollection
TA 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
inName
parameter.inName
System.StringThe name of the variable passed to the
inCollection
parameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
Returns
- T
The collection passed as parameter
inCollection
will be returned if it validates as being neither null nor empty.
Type Parameters
T
The type parameter
T
captures the type of the collection passed to the parameterinCollection
so that the value can be returned as exactly the same type if it validates successfully. The type should not have to be specified explicitly when calling the method. There are few constraints on the typeT
; it must be a class that implements the interface System.Collections.IEnumerable.
Exceptions
- System.ArgumentNullException
A System.ArgumentNullException will be thrown if the value of parameter
inCollection
is null.If the parameter
inName
has been correctly populated, the name of the collection that was found to be unexpectedly null will be included in the exception message.- System.ArgumentException
A System.ArgumentException will be thrown if the collection passed as parameter
inCollection
is empty.If the parameter
inName
has been correctly populated, the name of the collection that was found to be unexpectedly empty will be included in the exception message.
ThrowIfNullOrWhiteSpace(String, String)
inValue
, or an System.ArgumentException if the string exists but is either empty or
contains only white space characters.
public static string ThrowIfNullOrWhiteSpace(string inValue, string inName)
Parameters
inValue
System.StringA 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
inName
parameter.inName
System.StringThe name of the variable passed to the
inValue
parameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
Returns
- System.String
The string passed as parameter
inValue
will be returned if it validates as being neither null nor empty, nor containing only whitespace characters.
Exceptions
- System.ArgumentNullException
A System.ArgumentNullException will be thrown if the value of parameter
inValue
is null.If the parameter
inName
has been correctly populated, the name of the string that was found to be unexpectedly null will be included in the exception message.- System.ArgumentException
A System.ArgumentException will be thrown if the string passed as parameter
inValue
is empty or contains only white space characters.If the parameter
inName
has been correctly populated, the name of the string that was found to be unexpectedly empty or containing only white space will be included in the exception message.