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
- inPredicateSystem.Predicate<T>
- A predicate that must be satisfied by the value passed as - inValueif an exception is to be thrown.
- inValueT
- A 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 the- inNameparameter.
- inNameSystem.String
- The name of the variable passed to the - inValueparameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
- inMessageSystem.String
- A message to be included within the exception message, if it's thrown. 
- inMessageArgumentsSystem.Object[]
- Additional values to be included in the message - inMessage.
Returns
- T
- The value passed as parameter - inValuewill be returned if the predicate passed as value- inPredicateis not satisfied.
Type Parameters
- T
- The type parameter - Tcaptures the type of the value passed to the parameter- inValueso 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 type- T.
Exceptions
- System.ArgumentException
- An System.ArgumentException will be thrown if parameter - inValuesatisfies the predicate passed as- inPredicate.- If the parameter - inNamehas 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
- inValueSystem.Guid
- 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 - inNameparameter.
- inNameSystem.String
- The name of the variable passed to the - inValueparameter. 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 - inValuewill be returned if it's not the empty Guid.
Exceptions
- System.ArgumentException
- A System.ArgumentException will be thrown if the GUID - inValuehas the value System.Guid.Empty.- If the parameter - inNamehas 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
- inForbiddenValueT
- A value which the value - inValuecannot be equal to. If- inValueis equal to- inForbiddenValue, an System.ArgumentOutOfRangeException will be thrown.
- inValueT
- A 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 the- inNameparameter.
- inNameSystem.String
- The name of the variable passed to the - inValueparameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
Returns
- T
- The value passed as parameter - inValuewill be returned if it validates as not being equal to- inForbiddenValue.
Type Parameters
- T
- The type parameter - Tcaptures the type of the value passed to the parameter- inValueso 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 type- T; 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 - inValueis equal to the parameter- inForbiddenValue.- If the parameter - inNamehas been correctly populated, the name of the value that was found to be greater than- inForbiddenValuewill 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
- inLimitT
- A value which the value - inValuewill be compared against as a limit that cannot be exceeded. If- inValueis greater than- inLimit, an System.ArgumentOutOfRangeException will be thrown.
- inValueT
- A 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 the- inNameparameter.
- inNameSystem.String
- The name of the variable passed to the - inValueparameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
Returns
- T
- The value passed as parameter - inValuewill be returned if it validates as being less than or equal to- inLimit.
Type Parameters
- T
- The type parameter - Tcaptures the type of the value passed to the parameter- inValueso 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 type- T; it must implement the interface System.IComparable`1.
Exceptions
- System.ArgumentOutOfRangeException
- A System.ArgumentOutOfRangeException will be thrown if parameter - inValueis greater than the parameter- inLimit.- If the parameter - inNamehas been correctly populated, the name of the value that was found to be greater than- inLimitwill 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
- inLimitT
- A value which the value - inValuewill be compared against as a limit that cannot be equalled or exceeded. If- inValueis greater than or equal to- inLimit, an System.ArgumentOutOfRangeException will be thrown.
- inValueT
- A 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 the- inNameparameter.
- inNameSystem.String
- The name of the variable passed to the - inValueparameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
Returns
- T
- The value passed as parameter - inValuewill be returned if it validates as being less than- inLimit.
Type Parameters
- T
- The type parameter - Tcaptures the type of the value passed to the parameter- inValueso 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 type- T; it must implement the interface System.IComparable`1.
Exceptions
- System.ArgumentOutOfRangeException
- A System.ArgumentOutOfRangeException will be thrown if parameter - inValueis greater than or equal to the parameter- inLimit.- If the parameter - inNamehas been correctly populated, the name of the value that was found to be greater than- inLimitwill 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
- inLimitT
- A value which the value - inValuewill be compared against, as a lower limit that must be equalled or exceeded. If- inValueis less than- inLimit, an System.ArgumentOutOfRangeException will be thrown.
- inValueT
- A 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 the- inNameparameter.
- inNameSystem.String
- The name of the variable passed to the - inValueparameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
Returns
- T
- The value passed as parameter - inValuewill be returned if it validates as being less than- inLimit.
Type Parameters
- T
- The type parameter - Tcaptures the type of the value passed to the parameter- inValueso 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 type- T; it must implement the interface System.IComparable`1.
Exceptions
- System.ArgumentOutOfRangeException
- A System.ArgumentOutOfRangeException will be thrown if parameter - inValueis less than the parameter- inLimit.- If the parameter - inNamehas been correctly populated, the name of the value that was found to be less than- inLimitwill 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
- inLimitT
- A value which the value - inValuewill be compared against, as a lower limit that must be exceeded. If- inValueis less than or equal to- inLimit, an System.ArgumentOutOfRangeException will be thrown.
- inValueT
- A 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 the- inNameparameter.
- inNameSystem.String
- The name of the variable passed to the - inValueparameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
Returns
- T
- The value passed as parameter - inValuewill be returned if it validates as being less than or equal to- inLimit.
Type Parameters
- T
- The type parameter - Tcaptures the type of the value passed to the parameter- inValueso 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 type- T; it must implement the interface System.IComparable`1.
Exceptions
- System.ArgumentOutOfRangeException
- A System.ArgumentOutOfRangeException will be thrown if parameter - inValueis less than or equal to the parameter- inLimit.- If the parameter - inNamehas been correctly populated, the name of the value that was found to be less than- inLimitwill 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
- inPredicateSystem.Predicate<T>
- A predicate that must be satisfied by the value passed as - inValueif an exception is not to be thrown.
- inValueT
- A 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 the- inNameparameter.
- inNameSystem.String
- The name of the variable passed to the - inValueparameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
- inMessageSystem.String
- A message to be included within the exception message, if it's thrown. 
- inMessageArgumentsSystem.Object[]
- Additional values to be included in the message - inMessage.
Returns
- T
- The value passed as parameter - inValuewill be returned if the predicate passed as value- inPredicateis satisfied.
Type Parameters
- T
- The type parameter - Tcaptures the type of the value passed to the parameter- inValueso 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 type- T.
Exceptions
- System.ArgumentException
- An System.ArgumentException will be thrown if parameter - inValuedoes not satisfy the predicate passed as- inPredicate.- If the parameter - inNamehas 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
- inTypeSystem.Type
- The type - inTypespecifies that the- inActualshould be of either that type or one derived from it.
- inActualSystem.Object
- An object that should be of type - inTypeor a type derived from it. The name of the variable containing the object should be passed to this method as the- inNameparameter.
- inNameSystem.String
- The name of the variable passed to the - inActualparameter. 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 - inActualwill 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 - inActualis neither of the type- inTypenor of a type derived from it.- If the parameter - inNamehas 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 : classParameters
- inActualSystem.Object
- An object that should be of type - Tor a type derived from it. The name of the variable containing the object should be passed to this method as the- inNameparameter.
- inNameSystem.String
- The name of the variable passed to the - inActualparameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
Returns
- T
- The object passed as parameter - inActualwill be returned if it's of the expected type or a derived one.
Type Parameters
- T
- The type parameter - Tspecifies that the- inActualshould 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 - inActualis neither of the type- Tnor of a type derived from it.- If the parameter - inNamehas 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
- inValueT
- 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 - inNameparameter.
- inNameSystem.String
- The name of the variable passed to the - inValueparameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
Returns
- T
- The value passed as parameter - inValuewill be returned if it validates as non-null.
Type Parameters
- T
- The type parameter - Tcaptures the type of the parameter- inValueso 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 - inValueis null.- If the parameter - inNamehas 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
- inValueSystem.String
- 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 - inNameparameter.
- inNameSystem.String
- The name of the variable passed to the - inValueparameter. 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 - inValuewill 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 - inValueis null.- If the parameter - inNamehas 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 - inValueis empty.- If the parameter - inNamehas 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
- inValueSystem.String
- 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 - inNameparameter.
- inNameSystem.String
- The name of the variable passed to the - inValueparameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
- inTrimSystem.Boolean
- A 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 - inValuebefore returning it, while ThrowIfNullOrWhiteSpace(String, String) has no side effects.
Returns
- System.String
- The string passed as parameter - inValuewill be returned if it validates as being neither null nor empty. If the value of- inTrimwas- true, 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 - inValueis null.- If the parameter - inNamehas 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 - inValueis empty.- If the parameter - inNamehas 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, IEnumerableParameters
- inCollectionT
- 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 - inNameparameter.
- inNameSystem.String
- The name of the variable passed to the - inCollectionparameter. Since C# 6.0, the nameof operator can be used to retrieve a value to pass here.
Returns
- T
- The collection passed as parameter - inCollectionwill be returned if it validates as being neither null nor empty.
Type Parameters
- T
- The type parameter - Tcaptures the type of the collection passed to the parameter- inCollectionso 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 type- T; 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 - inCollectionis null.- If the parameter - inNamehas 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 - inCollectionis empty.- If the parameter - inNamehas 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
- inValueSystem.String
- 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 - inNameparameter.
- inNameSystem.String
- The name of the variable passed to the - inValueparameter. 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 - inValuewill 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 - inValueis null.- If the parameter - inNamehas 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 - inValueis empty or contains only white space characters.- If the parameter - inNamehas 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.