Gorgon
Show / Hide Table of Contents

Class GorgonDebugExtensions

Extension methods used to check values for correctness at runtime in debug mode.

Inheritance
object
GorgonDebugExtensions
Inherited Members
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
Namespace: Gorgon.Diagnostics
Assembly: Gorgon.Core.dll
Syntax
public static class GorgonDebugExtensions

Methods

| Edit this page View Source

ValidateIndex(ICollection, int)

Function to determine if a range is valid for a collection.

Declaration
[Conditional("DEBUG")]
public static void ValidateIndex(this ICollection collection, int index)
Parameters
Type Name Description
ICollection collection

The collection to evaluate.

int index

Index being requested.

Remarks

This will evaluate the Count property and check to see that the index parameter is not less than 0, and not greater than or equal to the count.

This will only throw exceptions when we're in DEBUG mode. Release mode will do nothing.

| Edit this page View Source

ValidateNullable<T>(T?, string)

Function to throw an exception if a Nullable<T> type is null.

Declaration
[Conditional("DEBUG")]
public static void ValidateNullable<T>(this T? value, string paramName) where T : struct
Parameters
Type Name Description
T? value

Value to evaluate.

string paramName

Name of the parameter to evaluate.

Type Parameters
Name Description
T

A reference type to evaluate. This must be a value type.

Remarks

This will evaluate the value of the nullable value type specified by T for null.

This will only throw exceptions when we're in DEBUG mode. Release mode will do nothing.

| Edit this page View Source

ValidateObject<T>(T, string)

Function to throw an exception if an object is null.

Declaration
[Conditional("DEBUG")]
public static void ValidateObject<T>(this T value, string paramName) where T : class
Parameters
Type Name Description
T value

Value to evaluate.

string paramName

Name of the parameter to evaluate.

Type Parameters
Name Description
T

A reference type to evaluate. This must be a reference type.

Remarks

This will evaluate the value of the type specified by T for null.

This will only throw exceptions when we're in DEBUG mode. Release mode will do nothing.

| Edit this page View Source

ValidateRange(byte, string, byte, byte, bool, bool)

Function to throw an exception if an byte value is not between the range specified.

Declaration
[Conditional("DEBUG")]
public static void ValidateRange(this byte value, string paramName, byte min, byte max, bool minInclusive = true, bool maxInclusive = false)
Parameters
Type Name Description
byte value

Value to compare.

string paramName

Name of the parameter.

byte min

Minimum value.

byte max

Maximum value.

bool minInclusive

[Optional] true if the minimum is inclusive in the range (i.e. value < min).

bool maxInclusive

[Optional] true if the maximum is inclusive in the range (i.e. value > max).

Remarks

When the minInclusive, or the maxInclusive are set to true, then the value is included in the range check. This means that the check is value < min for the minimum value, and value > max for the maximum value. If those parameters are set to false, then the range check is value <= min for the minimum value and value >= max for the maximum value.

This will only throw exceptions when we're in DEBUG mode. Release mode will do nothing.

| Edit this page View Source

ValidateRange(decimal, string, decimal, decimal, bool, bool)

Function to throw an exception if an decimal value is not between the range specified.

Declaration
[Conditional("DEBUG")]
public static void ValidateRange(this decimal value, string paramName, decimal min, decimal max, bool minInclusive = true, bool maxInclusive = false)
Parameters
Type Name Description
decimal value

Value to compare.

string paramName

Name of the parameter.

decimal min

Minimum value.

decimal max

Maximum value.

bool minInclusive

[Optional] true if the minimum is inclusive in the range (i.e. value < min).

bool maxInclusive

[Optional] true if the maximum is inclusive in the range (i.e. value > max).

Remarks

When the minInclusive, or the maxInclusive are set to true, then the value is included in the range check. This means that the check is value < min for the minimum value, and value > max for the maximum value. If those parameters are set to false, then the range check is value <= min for the minimum value and value >= max for the maximum value.

This will only throw exceptions when we're in DEBUG mode. Release mode will do nothing.

| Edit this page View Source

ValidateRange(double, string, double, double, bool, bool)

Function to throw an exception if an double value is not between the range specified.

Declaration
[Conditional("DEBUG")]
public static void ValidateRange(this double value, string paramName, double min, double max, bool minInclusive = true, bool maxInclusive = false)
Parameters
Type Name Description
double value

Value to compare.

string paramName

Name of the parameter.

double min

Minimum value.

double max

Maximum value.

bool minInclusive

[Optional] true if the minimum is inclusive in the range (i.e. value < min).

bool maxInclusive

[Optional] true if the maximum is inclusive in the range (i.e. value > max).

Remarks

When the minInclusive, or the maxInclusive are set to true, then the value is included in the range check. This means that the check is value < min for the minimum value, and value > max for the maximum value. If those parameters are set to false, then the range check is value <= min for the minimum value and value >= max for the maximum value.

This will only throw exceptions when we're in DEBUG mode. Release mode will do nothing.

| Edit this page View Source

ValidateRange(short, string, short, short, bool, bool)

Function to throw an exception if an short value is not between the range specified.

Declaration
[Conditional("DEBUG")]
public static void ValidateRange(this short value, string paramName, short min, short max, bool minInclusive = true, bool maxInclusive = false)
Parameters
Type Name Description
short value

Value to compare.

string paramName

Name of the parameter.

short min

Minimum value.

short max

Maximum value.

bool minInclusive

[Optional] true if the minimum is inclusive in the range (i.e. value < min).

bool maxInclusive

[Optional] true if the maximum is inclusive in the range (i.e. value > max).

Remarks

When the minInclusive, or the maxInclusive are set to true, then the value is included in the range check. This means that the check is value < min for the minimum value, and value > max for the maximum value. If those parameters are set to false, then the range check is value <= min for the minimum value and value >= max for the maximum value.

This will only throw exceptions when we're in DEBUG mode. Release mode will do nothing.

| Edit this page View Source

ValidateRange(int, string, int, int, bool, bool)

Function to throw an exception if an int value is not between the range specified.

Declaration
[Conditional("DEBUG")]
public static void ValidateRange(this int value, string paramName, int min, int max, bool minInclusive = true, bool maxInclusive = false)
Parameters
Type Name Description
int value

Value to compare.

string paramName

Name of the parameter.

int min

Minimum value.

int max

Maximum value.

bool minInclusive

[Optional] true if the minimum is inclusive in the range (i.e. value < min).

bool maxInclusive

[Optional] true if the maximum is inclusive in the range (i.e. value > max).

Remarks

When the minInclusive, or the maxInclusive are set to true, then the value is included in the range check. This means that the check is value < min for the minimum value, and value > max for the maximum value. If those parameters are set to false, then the range check is value <= min for the minimum value and value >= max for the maximum value.

This will only throw exceptions when we're in DEBUG mode. Release mode will do nothing.

| Edit this page View Source

ValidateRange(long, string, long, long, bool, bool)

Function to throw an exception if an long value is not between the range specified.

Declaration
[Conditional("DEBUG")]
public static void ValidateRange(this long value, string paramName, long min, long max, bool minInclusive = true, bool maxInclusive = false)
Parameters
Type Name Description
long value

Value to compare.

string paramName

Name of the parameter.

long min

Minimum value.

long max

Maximum value.

bool minInclusive

[Optional] true if the minimum is inclusive in the range (i.e. value < min).

bool maxInclusive

[Optional] true if the maximum is inclusive in the range (i.e. value > max).

Remarks

When the minInclusive, or the maxInclusive are set to true, then the value is included in the range check. This means that the check is value < min for the minimum value, and value > max for the maximum value. If those parameters are set to false, then the range check is value <= min for the minimum value and value >= max for the maximum value.

This will only throw exceptions when we're in DEBUG mode. Release mode will do nothing.

| Edit this page View Source

ValidateRange(sbyte, string, sbyte, sbyte, bool, bool)

Function to throw an exception if an sbyte value is not between the range specified.

Declaration
[Conditional("DEBUG")]
public static void ValidateRange(this sbyte value, string paramName, sbyte min, sbyte max, bool minInclusive = true, bool maxInclusive = false)
Parameters
Type Name Description
sbyte value

Value to compare.

string paramName

Name of the parameter.

sbyte min

Minimum value.

sbyte max

Maximum value.

bool minInclusive

[Optional] true if the minimum is inclusive in the range (i.e. value < min).

bool maxInclusive

[Optional] true if the maximum is inclusive in the range (i.e. value > max).

Remarks

When the minInclusive, or the maxInclusive are set to true, then the value is included in the range check. This means that the check is value < min for the minimum value, and value > max for the maximum value. If those parameters are set to false, then the range check is value <= min for the minimum value and value >= max for the maximum value.

This will only throw exceptions when we're in DEBUG mode. Release mode will do nothing.

| Edit this page View Source

ValidateRange(float, string, float, float, bool, bool)

Function to throw an exception if an float value is not between the range specified.

Declaration
[Conditional("DEBUG")]
public static void ValidateRange(this float value, string paramName, float min, float max, bool minInclusive = true, bool maxInclusive = false)
Parameters
Type Name Description
float value

Value to compare.

string paramName

Name of the parameter.

float min

Minimum value.

float max

Maximum value.

bool minInclusive

[Optional] true if the minimum is inclusive in the range (i.e. value < min).

bool maxInclusive

[Optional] true if the maximum is inclusive in the range (i.e. value > max).

Remarks

When the minInclusive, or the maxInclusive are set to true, then the value is included in the range check. This means that the check is value < min for the minimum value, and value > max for the maximum value. If those parameters are set to false, then the range check is value <= min for the minimum value and value >= max for the maximum value.

This will only throw exceptions when we're in DEBUG mode. Release mode will do nothing.

| Edit this page View Source

ValidateRange(ushort, string, ushort, ushort, bool, bool)

Function to throw an exception if an ushort value is not between the range specified.

Declaration
[Conditional("DEBUG")]
public static void ValidateRange(this ushort value, string paramName, ushort min, ushort max, bool minInclusive = true, bool maxInclusive = false)
Parameters
Type Name Description
ushort value

Value to compare.

string paramName

Name of the parameter.

ushort min

Minimum value.

ushort max

Maximum value.

bool minInclusive

[Optional] true if the minimum is inclusive in the range (i.e. value < min).

bool maxInclusive

[Optional] true if the maximum is inclusive in the range (i.e. value > max).

Remarks

When the minInclusive, or the maxInclusive are set to true, then the value is included in the range check. This means that the check is value < min for the minimum value, and value > max for the maximum value. If those parameters are set to false, then the range check is value <= min for the minimum value and value >= max for the maximum value.

This will only throw exceptions when we're in DEBUG mode. Release mode will do nothing.

| Edit this page View Source

ValidateRange(uint, string, uint, uint, bool, bool)

Function to throw an exception if an uint value is not between the range specified.

Declaration
[Conditional("DEBUG")]
public static void ValidateRange(this uint value, string paramName, uint min, uint max, bool minInclusive = true, bool maxInclusive = false)
Parameters
Type Name Description
uint value

Value to compare.

string paramName

Name of the parameter.

uint min

Minimum value.

uint max

Maximum value.

bool minInclusive

[Optional] true if the minimum is inclusive in the range (i.e. value < min).

bool maxInclusive

[Optional] true if the maximum is inclusive in the range (i.e. value > max).

Remarks

When the minInclusive, or the maxInclusive are set to true, then the value is included in the range check. This means that the check is value < min for the minimum value, and value > max for the maximum value. If those parameters are set to false, then the range check is value <= min for the minimum value and value >= max for the maximum value.

This will only throw exceptions when we're in DEBUG mode. Release mode will do nothing.

| Edit this page View Source

ValidateRange(ulong, string, ulong, ulong, bool, bool)

Function to throw an exception if an ulong value is not between the range specified.

Declaration
[Conditional("DEBUG")]
public static void ValidateRange(this ulong value, string paramName, ulong min, ulong max, bool minInclusive = true, bool maxInclusive = false)
Parameters
Type Name Description
ulong value

Value to compare.

string paramName

Name of the parameter.

ulong min

Minimum value.

ulong max

Maximum value.

bool minInclusive

[Optional] true if the minimum is inclusive in the range (i.e. value < min).

bool maxInclusive

[Optional] true if the maximum is inclusive in the range (i.e. value > max).

Remarks

When the minInclusive, or the maxInclusive are set to true, then the value is included in the range check. This means that the check is value < min for the minimum value, and value > max for the maximum value. If those parameters are set to false, then the range check is value <= min for the minimum value and value >= max for the maximum value.

This will only throw exceptions when we're in DEBUG mode. Release mode will do nothing.

| Edit this page View Source

ValidateString(string, string, bool)

Function to throw an exception if a string is null or empty.

Declaration
[Conditional("DEBUG")]
public static void ValidateString(this string value, string paramName, bool keepWhitespace = false)
Parameters
Type Name Description
string value

The value being passed.

string paramName

The name of the parameter.

bool keepWhitespace

[Optional] true to include whitespace when checking for an empty string, false if whitespace should be excluded when checking for an empty string.

Remarks

When the keepWhitespace value is set to true, then a value of " " (5 spaces in this example) will not throw an exception, but if the parameter is set to false, then an exception will be thrown as the string will be considered empty regardless of whitespace.

This will only throw exceptions when we're in DEBUG mode. Release mode will do nothing.

  • Edit this page
  • View Source
In this article
Back to top Copyright 2023 - Licensed under the MIT license by Michael Winsor (Tape_Worm).
Send comments on this topic to the author