Show / Hide Table of Contents

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
System.Object
Validate
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()
Namespace: Existential
Assembly: Existential.Net.dll
Syntax
public static class Validate

Methods

View Source

ThrowIf<T>(Predicate<T>, T, String, String, Object[])

ThrowIf<T>(Predicate<T>, T, String, String, Object[]) will throw an System.ArgumentException if the value passed as the parameter 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 inValue if an exception is to be thrown.

T inValue

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 inName parameter.

System.String inName

The 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.

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 inMessage.

Returns
Type Description
T

The value passed as parameter inValue will be returned if the predicate passed as value inPredicate is not satisfied.

Type Parameters
Name Description
T

The type parameter T captures the type of the value passed to the parameter inValue 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 type T.

Exceptions
Type Condition
System.ArgumentException

An System.ArgumentException will be thrown if parameter inValue satisfies the predicate passed as inPredicate.

If the parameter inName has been correctly populated, the name of the value will be included in the exception message.

View Source

ThrowIfEmptyGuid(Guid, String)

ThrowIfEmptyGuid(Guid, String) will throw an System.ArgumentNullException if passed an empty GUID (System.Guid.Empty) as the parameter 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 inName parameter.

System.String inName

The 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
Type Description
System.Guid

The value passed as parameter inValue will be returned if it's not the empty Guid.

Exceptions
Type Condition
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.

View Source

ThrowIfEqualTo<T>(T, T, String)

ThrowIfEqualTo<T>(T, T, String) will throw an System.ArgumentOutOfRangeException if the value passed as the parameter 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 inValue cannot be equal to. If inValue is equal to inForbiddenValue, an System.ArgumentOutOfRangeException will be thrown.

T inValue

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 inName parameter.

System.String inName

The 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
Type Description
T

The value passed as parameter inValue will be returned if it validates as not being equal to inForbiddenValue.

Type Parameters
Name Description
T

The type parameter T captures the type of the value passed to the parameter inValue 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 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
Type Condition
System.ArgumentOutOfRangeException

A System.ArgumentOutOfRangeException will be thrown if parameter inValue is equal to the parameter inForbiddenValue.

If the parameter inName has been correctly populated, the name of the value that was found to be greater than inForbiddenValue will be included in the exception message.

View Source

ThrowIfGreaterThan<T>(T, T, String)

ThrowIfGreaterThan<T>(T, T, String) will throw an System.ArgumentOutOfRangeException if the value passed as the parameter 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 inValue will be compared against as a limit that cannot be exceeded. If inValue is greater than inLimit, an System.ArgumentOutOfRangeException will be thrown.

T inValue

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 inName parameter.

System.String inName

The 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
Type Description
T

The value passed as parameter inValue will be returned if it validates as being less than or equal to inLimit.

Type Parameters
Name Description
T

The type parameter T captures the type of the value passed to the parameter inValue 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 type T; it must implement the interface System.IComparable`1.

Exceptions
Type Condition
System.ArgumentOutOfRangeException

A System.ArgumentOutOfRangeException will be thrown if parameter inValue is greater than the parameter inLimit.

If the parameter inName has been correctly populated, the name of the value that was found to be greater than inLimit will be included in the exception message.

View Source

ThrowIfGreaterThanOrEqualTo<T>(T, T, String)

ThrowIfGreaterThanOrEqualTo<T>(T, T, String) will throw an System.ArgumentOutOfRangeException if the value passed as the parameter 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 inValue will be compared against as a limit that cannot be equalled or exceeded. If inValue is greater than or equal to inLimit, an System.ArgumentOutOfRangeException will be thrown.

T inValue

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 inName parameter.

System.String inName

The 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
Type Description
T

The value passed as parameter inValue will be returned if it validates as being less than inLimit.

Type Parameters
Name Description
T

The type parameter T captures the type of the value passed to the parameter inValue 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 type T; it must implement the interface System.IComparable`1.

Exceptions
Type Condition
System.ArgumentOutOfRangeException

A System.ArgumentOutOfRangeException will be thrown if parameter inValue is greater than or equal to the parameter inLimit.

If the parameter inName has been correctly populated, the name of the value that was found to be greater than inLimit will be included in the exception message.

View Source

ThrowIfLessThan<T>(T, T, String)

ThrowIfLessThan<T>(T, T, String) will throw an System.ArgumentOutOfRangeException if the value passed as the parameter 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 inValue will be compared against, as a lower limit that must be equalled or exceeded. If inValue is less than inLimit, an System.ArgumentOutOfRangeException will be thrown.

T inValue

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 inName parameter.

System.String inName

The 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
Type Description
T

The value passed as parameter inValue will be returned if it validates as being less than inLimit.

Type Parameters
Name Description
T

The type parameter T captures the type of the value passed to the parameter inValue 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 type T; it must implement the interface System.IComparable`1.

Exceptions
Type Condition
System.ArgumentOutOfRangeException

A System.ArgumentOutOfRangeException will be thrown if parameter inValue is less than the parameter inLimit.

If the parameter inName has been correctly populated, the name of the value that was found to be less than inLimit will be included in the exception message.

View Source

ThrowIfLessThanOrEqualTo<T>(T, T, String)

ThrowIfLessThanOrEqualTo<T>(T, T, String) will throw an System.ArgumentOutOfRangeException if the value passed as the parameter 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 inValue will be compared against, as a lower limit that must be exceeded. If inValue is less than or equal to inLimit, an System.ArgumentOutOfRangeException will be thrown.

T inValue

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 inName parameter.

System.String inName

The 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
Type Description
T

The value passed as parameter inValue will be returned if it validates as being less than or equal to inLimit.

Type Parameters
Name Description
T

The type parameter T captures the type of the value passed to the parameter inValue 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 type T; it must implement the interface System.IComparable`1.

Exceptions
Type Condition
System.ArgumentOutOfRangeException

A System.ArgumentOutOfRangeException will be thrown if parameter inValue is less than or equal to the parameter inLimit.

If the parameter inName has been correctly populated, the name of the value that was found to be less than inLimit will be included in the exception message.

View Source

ThrowIfNot<T>(Predicate<T>, T, String, String, Object[])

ThrowIfNot<T>(Predicate<T>, T, String, String, Object[]) will throw an System.ArgumentException if the value passed as the parameter 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 inValue if an exception is not to be thrown.

T inValue

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 inName parameter.

System.String inName

The 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.

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 inMessage.

Returns
Type Description
T

The value passed as parameter inValue will be returned if the predicate passed as value inPredicate is satisfied.

Type Parameters
Name Description
T

The type parameter T captures the type of the value passed to the parameter inValue 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 type T.

Exceptions
Type Condition
System.ArgumentException

An System.ArgumentException will be thrown if parameter inValue does not satisfy the predicate passed as inPredicate.

If the parameter inName has been correctly populated, the name of the value will be included in the exception message.

View Source

ThrowIfNotOfType(Type, Object, String)

ThrowIfNotOfType<T>(Object, String) will throw an ArgumentTypeException if the object passed as the parameter 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 inType specifies that the inActual should be of either that type or one derived from it.

System.Object inActual

An 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 the inName parameter.

System.String inName

The 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
Type Description
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
Type Condition
ArgumentTypeException

A ArgumentTypeException will be thrown if the object inActual is neither of the type inType 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.

View Source

ThrowIfNotOfType<T>(Object, String)

ThrowIfNotOfType<T>(Object, String) will throw an ArgumentTypeException if the object passed as the parameter 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 T or a type derived from it. The name of the variable containing the object should be passed to this method as the inName parameter.

System.String inName

The 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
Type Description
T

The object passed as parameter inActual will be returned if it's of the expected type or a derived one.

Type Parameters
Name Description
T

The type parameter T specifies that the inActual 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
Type Condition
ArgumentTypeException

A ArgumentTypeException will be thrown if the object inActual is neither of the type T 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.

View Source

ThrowIfNull<T>(T, String)

ThrowIfNull<T>(T, String) will throw an System.ArgumentNullException if the value passed as the parameter 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 inName parameter.

System.String inName

The 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
Type Description
T

The value passed as parameter inValue will be returned if it validates as non-null.

Type Parameters
Name Description
T

The type parameter T captures the type of the parameter inValue 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
Type Condition
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.

View Source

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 inName parameter.

System.String inName

The 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
Type Description
System.String

The string passed as parameter inValue will be returned if it validates as being neither null nor empty.

Exceptions
Type Condition
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
ThrowIfNullOrWhiteSpace(String, String)
View Source

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.

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 inName parameter.

System.String inName

The 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.

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 inValue before returning it, while ThrowIfNullOrWhiteSpace(String, String) has no side-effects.

Returns
Type Description
System.String

The string passed as parameter inValue will be returned if it validates as being neither null nor empty. If the value of inTrim was true, then white space characters will be trimmed from either end of the string before it is returned.

Exceptions
Type Condition
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
ThrowIfNullOrWhiteSpace(String, String)
View Source

ThrowIfNullOrEmpty<T>(T, String)

ThrowIfNullOrEmpty<T>(T, String) will throw an System.ArgumentNullException if a null value is passed as the parameter 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 inName parameter.

System.String inName

The 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
Type Description
T

The collection passed as parameter inCollection will be returned if it validates as being neither null nor empty.

Type Parameters
Name Description
T

The type parameter T captures the type of the collection passed to the parameter inCollection 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 type T; it must be a class that implements the interface System.Collections.IEnumerable.

Exceptions
Type Condition
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.

View Source

ThrowIfNullOrWhiteSpace(String, String)

ThrowIfNullOrWhiteSpace(String, String) 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 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 inName parameter.

System.String inName

The 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
Type Description
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
Type Condition
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.

  • View Source
In This Article
Back to top Copyright © 2021 Dr. Gavin T.D. Greig.