Table of Contents

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.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()

Methods

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

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.

inName System.String

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.

inMessage System.String

A 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 value inPredicate is not satisfied.

Type Parameters

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

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.

ThrowIfEmptyGuid(Guid, String)

ThrowIfEmptyGuid(Guid, String) will throw an System.ArgumentNullException if passed an empty GUID (System.Guid.Empty) as the parameter inValue.
public static Guid ThrowIfEmptyGuid(Guid inValue, string inName)

Parameters

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

inName System.String

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

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)

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.
public static T ThrowIfEqualTo<T>(T inForbiddenValue, T inValue, string inName)

    where T : IComparable<T>

Parameters

inForbiddenValue T

A value which the value inValue cannot be equal to. If inValue is equal to inForbiddenValue, an System.ArgumentOutOfRangeException will be thrown.

inValue T

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.

inName System.String

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

T

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

Type Parameters

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

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.

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.
public static T ThrowIfGreaterThan<T>(T inLimit, T inValue, string inName)

    where T : IComparable<T>

Parameters

inLimit T

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.

inValue T

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.

inName System.String

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

T

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

Type Parameters

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

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.

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.
public static T ThrowIfGreaterThanOrEqualTo<T>(T inLimit, T inValue, string inName)

    where T : IComparable<T>

Parameters

inLimit T

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.

inValue T

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.

inName System.String

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

T

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

Type Parameters

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

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.

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.
public static T ThrowIfLessThan<T>(T inLimit, T inValue, string inName)

    where T : IComparable<T>

Parameters

inLimit T

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.

inValue T

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.

inName System.String

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

T

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

Type Parameters

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

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.

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.
public static T ThrowIfLessThanOrEqualTo<T>(T inLimit, T inValue, string inName)

    where T : IComparable<T>

Parameters

inLimit T

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.

inValue T

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.

inName System.String

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

T

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

Type Parameters

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

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.

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

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.

inName System.String

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.

inMessage System.String

A 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 value inPredicate is satisfied.

Type Parameters

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

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.

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.
public static object ThrowIfNotOfType(Type inType, object inActual, string inName)

Parameters

inType System.Type

The type inType specifies that the inActual should be of either that type or one derived from it.

inActual System.Object

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.

inName System.String

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

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

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.
public static T ThrowIfNotOfType<T>(object inActual, string inName)

    where T : class

Parameters

inActual System.Object

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.

inName System.String

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

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

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.

ThrowIfNull<T>(T, String)

ThrowIfNull<T>(T, String) will throw an System.ArgumentNullException if the value passed as the parameter inValue is null.
public static T ThrowIfNull<T>(T inValue, string inName)

Parameters

inValue T

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.

inName System.String

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

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

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

inName System.String

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

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

inName System.String

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.

inTrim System.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 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 of inTrim was 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 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)

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.
public static T ThrowIfNullOrEmpty<T>(T inCollection, string inName)

    where T : class, IEnumerable

Parameters

inCollection T

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.

inName System.String

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

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

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)

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.
public static string ThrowIfNullOrWhiteSpace(string inValue, string inName)

Parameters

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

inName System.String

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

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.