Class Guard
Helper class for guarding value arguments and valid state.
public static class Guard
- Inheritance
-
Guard
- Inherited Members
Methods
ArgumentEnumValid<T>(T, HashSet<T>, string?)
Ensures that an enum value is valid by comparing against a list of valid values.
public static T ArgumentEnumValid<T>(T argValue, HashSet<T> validValues, string? argName = null) where T : Enum
Parameters
argValueTThe value of the argument
validValuesHashSet<T>The list of valid values
argNamestringThe name of the argument
Returns
- T
Type Parameters
TThe argument type
Exceptions
ArgumentNotNullOrEmpty<T>(Func<string>, T?, string?)
Ensures that a nullable enumerable type argument is not null or empty.
public static T ArgumentNotNullOrEmpty<T>(Func<string> messageFunc, T? argValue, string? argName = null) where T : class, IEnumerable
Parameters
messageFuncFunc<string>The creator for an exception message to use when the argument is null or empty
argValueTThe value of the argument
argNamestringThe name of the argument
Returns
- T
The argument value as a non-null, non-empty value
Type Parameters
TThe argument type
Exceptions
- ArgumentException
Thrown when the argument is null or empty
ArgumentNotNullOrEmpty<T>(string, T?, string?)
Ensures that a nullable enumerable type argument is not null or empty.
public static T ArgumentNotNullOrEmpty<T>(string message, T? argValue, string? argName = null) where T : class, IEnumerable
Parameters
messagestringThe exception message to use when the argument is null or empty
argValueTThe value of the argument
argNamestringThe name of the argument
Returns
- T
The argument value as a non-null, non-empty value
Type Parameters
TThe argument type
Exceptions
- ArgumentException
Thrown when the argument is null or empty
ArgumentNotNullOrEmpty<T>(T?, string?)
Ensures that a nullable enumerable type argument is not null or empty.
public static T ArgumentNotNullOrEmpty<T>(T? argValue, string? argName = null) where T : class, IEnumerable
Parameters
argValueTThe value of the argument
argNamestringThe name of the argument
Returns
- T
The argument value as a non-null, non-empty value
Type Parameters
TThe argument type
Exceptions
- ArgumentException
Thrown when the argument is null or empty
ArgumentNotNull<T>(Func<string>, T?, string?)
Ensures that a nullable reference type argument is not null.
public static T ArgumentNotNull<T>(Func<string> messageFunc, T? argValue, string? argName = null) where T : class
Parameters
messageFuncFunc<string>The creator for an exception message to use when the argument is null
argValueTThe value of the argument
argNamestringThe name of the argument
Returns
- T
The argument value as a non-null value
Type Parameters
TThe argument type
Exceptions
- ArgumentNullException
Thrown when the argument is null
ArgumentNotNull<T>(T?, string?)
Ensures that a nullable value type argument is not null.
public static T ArgumentNotNull<T>(T? argValue, string? argName = null) where T : struct
Parameters
argValueT?The value of the argument
argNamestringThe name of the argument
Returns
- T
The argument value as a non-null value
Type Parameters
TThe argument type
Exceptions
- ArgumentNullException
Thrown when the argument is null
ArgumentNotNull<T>(string, T?, string?)
Ensures that a nullable reference type argument is not null.
public static T ArgumentNotNull<T>(string message, T? argValue, string? argName = null) where T : class
Parameters
messagestringThe exception message to use when the argument is null
argValueTThe value of the argument
argNamestringThe name of the argument
Returns
- T
The argument value as a non-null value
Type Parameters
TThe argument type
Exceptions
- ArgumentNullException
Thrown when the argument is null
ArgumentNotNull<T>(T?, string?)
Ensures that a nullable reference type argument is not null.
public static T ArgumentNotNull<T>(T? argValue, string? argName = null) where T : class
Parameters
argValueTThe value of the argument
argNamestringThe name of the argument
Returns
- T
The argument value as a non-null value
Type Parameters
TThe argument type
Exceptions
- ArgumentNullException
Thrown when the argument is null
ArgumentValid(Func<string>, bool, string?)
Ensures that an argument is valid.
public static void ArgumentValid(Func<string> messageFunc, bool test, string? argName = null)
Parameters
messageFuncFunc<string>The creator for an exception message to use when the argument is not valid
testboolThe validity test value
argNamestringThe name of the argument
Exceptions
- ArgumentException
Thrown when the argument is not valid
ArgumentValid(string, bool, string?)
Ensures that an argument is valid.
public static void ArgumentValid(string message, bool test, string? argName = null)
Parameters
messagestringThe exception message to use when the argument is not valid
testboolThe validity test value
argNamestringThe name of the argument
Exceptions
- ArgumentException
Thrown when the argument is not valid
FileExists(string?, string?)
Ensures that a filename argument is not null or empty, and that the file exists on disk.
public static string FileExists(string? fileName, string? argName = null)
Parameters
Returns
- string
The file name as a non-null value
Exceptions
- ArgumentException
Thrown when the argument is null, empty, or not on disk
GenericArgumentNotNull<T>(T?, string?)
Ensures that a value is not default value. This is used for values of generic types where nullability is not known.
public static T GenericArgumentNotNull<T>(T? argValue, string? argName = null)
Parameters
argValueTThe value of the argument
argNamestringThe name of the argument
Returns
- T
The argument value as a non-default value
Type Parameters
TThe argument type
Exceptions
- ArgumentNullException
Thrown when the argument is default
NotNull<T>(Func<string>, T?)
Ensure that a nullable struct value is not null.
public static T NotNull<T>(Func<string> messageFunc, T? value) where T : struct
Parameters
messageFuncFunc<string>The creator for an exception message to use when the value is not valid
valueT?The value to test for null
Returns
- T
The value as a non-null value
Type Parameters
TThe value type
Exceptions
- InvalidOperationException
Thrown when the value is not valid
NotNull<T>(Func<string>, T?)
Ensure that a reference value is not null.
public static T NotNull<T>(Func<string> messageFunc, T? value) where T : class
Parameters
messageFuncFunc<string>The creator for an exception message to use when the value is not valid
valueTThe value to test for null
Returns
- T
The value as a non-null value
Type Parameters
TThe value type
Exceptions
- InvalidOperationException
Thrown when the value is not valid
NotNull<T>(string, T?)
Ensure that a nullable struct value is not null.
public static T NotNull<T>(string message, T? value) where T : struct
Parameters
messagestringThe exception message to use when the value is not valid
valueT?The value to test for null
Returns
- T
The value as a non-null value
Type Parameters
TThe value type
Exceptions
- InvalidOperationException
Thrown when the value is not valid
NotNull<T>(string, T?)
Ensure that a reference value is not null.
public static T NotNull<T>(string message, T? value) where T : class
Parameters
messagestringThe exception message to use when the value is not valid
valueTThe value to test for null
Returns
- T
The value as a non-null value
Type Parameters
TThe value type
Exceptions
- InvalidOperationException
Thrown when the value is not valid