Table of Contents

Class Guard

Namespace
Xunit.Internal

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

argValue T

The value of the argument

validValues HashSet<T>

The list of valid values

argName string

The name of the argument

Returns

T

Type Parameters

T

The argument type

Exceptions

ArgumentException

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

messageFunc Func<string>

The creator for an exception message to use when the argument is null or empty

argValue T

The value of the argument

argName string

The name of the argument

Returns

T

The argument value as a non-null, non-empty value

Type Parameters

T

The 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

message string

The exception message to use when the argument is null or empty

argValue T

The value of the argument

argName string

The name of the argument

Returns

T

The argument value as a non-null, non-empty value

Type Parameters

T

The 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

argValue T

The value of the argument

argName string

The name of the argument

Returns

T

The argument value as a non-null, non-empty value

Type Parameters

T

The 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

messageFunc Func<string>

The creator for an exception message to use when the argument is null

argValue T

The value of the argument

argName string

The name of the argument

Returns

T

The argument value as a non-null value

Type Parameters

T

The 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

argValue T?

The value of the argument

argName string

The name of the argument

Returns

T

The argument value as a non-null value

Type Parameters

T

The 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

message string

The exception message to use when the argument is null

argValue T

The value of the argument

argName string

The name of the argument

Returns

T

The argument value as a non-null value

Type Parameters

T

The 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

argValue T

The value of the argument

argName string

The name of the argument

Returns

T

The argument value as a non-null value

Type Parameters

T

The 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

messageFunc Func<string>

The creator for an exception message to use when the argument is not valid

test bool

The validity test value

argName string

The 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

message string

The exception message to use when the argument is not valid

test bool

The validity test value

argName string

The 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

fileName string

The file name value

argName string

The name of the argument

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

argValue T

The value of the argument

argName string

The name of the argument

Returns

T

The argument value as a non-default value

Type Parameters

T

The 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

messageFunc Func<string>

The creator for an exception message to use when the value is not valid

value T?

The value to test for null

Returns

T

The value as a non-null value

Type Parameters

T

The 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

messageFunc Func<string>

The creator for an exception message to use when the value is not valid

value T

The value to test for null

Returns

T

The value as a non-null value

Type Parameters

T

The 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

message string

The exception message to use when the value is not valid

value T?

The value to test for null

Returns

T

The value as a non-null value

Type Parameters

T

The 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

message string

The exception message to use when the value is not valid

value T

The value to test for null

Returns

T

The value as a non-null value

Type Parameters

T

The value type

Exceptions

InvalidOperationException

Thrown when the value is not valid