Contract

Namespace: SnowBank.Diagnostics.Contracts · class

Helper type to check for pre-requisites, invariants, assertions, ...

Properties

IsUnitTesting

static bool IsUnitTesting { get; set; }

Methods

Assert

static void Assert(bool condition, string userMessage = null, string conditionText = null)

Test if a condition is true, inside the body of a method.

  • condition — Condition that should never be false
  • userMessage — Message that describes the failed assertion (optional)
  • conditionText — Text of the condition (optional, injected by the compiler)

No-op if is true. Otherwise, throws a ContractException, after attempting to breakpoint (if a debugger is attached)

Between

static void Between<T>(T value, T minimumInclusive, T maximumInclusive, string message = null, string valueExpression = null, string minExpression = null, string maxExpression = null)

The specified value must not be outside the specified bounds (assert: min <= value <= max)

DoesNotOverflow

static void DoesNotOverflow<TElement>(ArraySegment<TElement> buffer, string message = null, string paramName = null)

The specified region must not be outside the specified buffer

static void DoesNotOverflow(int bufferLength, int offset, int count, string message = null)

The specified region must not be outside the specified buffer

static void DoesNotOverflow(long bufferLength, long offset, long count, string message = null)

The specified region must not be outside the specified buffer

static void DoesNotOverflow(string buffer, int index, int count, string message = null, string paramBuffer = null, string paramIndex = null, string paramCount = null)

The specified region must not be outside the specified buffer

static void DoesNotOverflow<TElement>(TElement[] buffer, int offset, int count, string message = null, string paramBuffer = null, string paramOffset = null, string paramCount = null)

The specified region must not be outside the specified buffer

static void DoesNotOverflow<TElement>(ICollection<TElement> buffer, int offset, int count, string message = null, string paramBuffer = null, string paramOffset = null, string paramCount = null)

The specified region must not be outside the specified buffer

Ensures

static void Ensures(bool condition, string userMessage = null, string conditionText = null)

Test if a post-condition is true, at the end of a method.

  • condition — Condition that should never be false
  • userMessage — Message that describes the failed assertion (optional)
  • conditionText — Text of the condition (optional, injected by the compiler)

No-op if is true. Otherwise, throws a ContractException, after attempting to breakpoint (if a debugger is attached)

EqualTo

static void EqualTo(int value, int expected, string message = null, string paramName = null)

The specified value must not equal to the specified constant (assert: value != forbidden)

static void EqualTo(long value, long expected, string message = null, string paramName = null)

The specified value must not equal to the specified constant (assert: value != forbidden)

static void EqualTo(uint value, uint expected, string message = null, string paramName = null)

The specified value must not equal to the specified constant (assert: value != forbidden)

static void EqualTo(ulong value, ulong expected, string message = null, string paramName = null)

The specified value must not equal to the specified constant (assert: value != forbidden)

static void EqualTo(string value, string expected, string message = null, string paramName = null)

The specified value must not equal to the specified constant (assert: value != forbidden)

static void EqualTo<T>(T value, T expected, string message = null, string paramName = null)

The specified value must not equal to the specified constant (assert: value != forbidden)

Fail

static void Fail(string userMessage, Exception exception = null)

Unconditionally trigger an assertion fault

  • userMessage — Message that describes the failed assertion (optional)
  • exception — Optional exception linked to the issue

Throws a , after attempting to breakpoint (if a debugger is attached)

FailArgumentExpected

static Exception FailArgumentExpected<T>(string paramName, T expected, string message = null)

FailArgumentForbidden

static Exception FailArgumentForbidden<T>(string paramName, T forbidden, string message = null)

FailArgumentNotGreaterOrEqual

static Exception FailArgumentNotGreaterOrEqual<T>(T value, string valueExpression, string thresholdExpression, bool zero, string message = null)

FailArgumentNotGreaterThan

static Exception FailArgumentNotGreaterThan<T>(T value, string valueExpression, string thresholdExpression, bool zero, string message = null)

FailArgumentNotLessOrEqual

static Exception FailArgumentNotLessOrEqual<T>(T value, string valueExpression, string thresholdExpression, string message = null)

FailArgumentNotLessThan

static Exception FailArgumentNotLessThan<T>(T value, string valueExpression, string thresholdExpression, string message = null)

FailArgumentNotMultiple

static Exception FailArgumentNotMultiple(string paramName, string message = null)

FailArgumentNotNonNegative

static Exception FailArgumentNotNonNegative(string paramName, string message = null)

FailArgumentNotPositive

static Exception FailArgumentNotPositive(string paramName, string message = null)

FailArgumentNotPowerOfTwo

static Exception FailArgumentNotPowerOfTwo(string paramName, string message = null)

FailArgumentNull

static Exception FailArgumentNull(string paramName, string message = null)

FailArgumentOutOfBounds

static Exception FailArgumentOutOfBounds(string paramName, string message = null)

static Exception FailArgumentOutOfBounds(string valueExpression, string minExpression, string maxExpression, string message = null)

FailArrayNullOrEmpty

static Exception FailArrayNullOrEmpty(object collection, string paramName, string message = null)

FailBufferEmpty

static Exception FailBufferEmpty(string paramName, string message = null)

FailBufferNull

static Exception FailBufferNull(string paramName, string message = null)

FailBufferNullOrEmpty

static Exception FailBufferNullOrEmpty(object array, string paramName, string message = null)

FailBufferTooSmall

static Exception FailBufferTooSmall(string paramName, string message = null)

FailCollectionNullOrEmpty

static Exception FailCollectionNullOrEmpty(object collection, string paramName, string message = null)

FailStringNullOrEmpty

static Exception FailStringNullOrEmpty(string value, string paramName, string message = null)

FailStringNullOrWhiteSpace

static Exception FailStringNullOrWhiteSpace(string value, string paramName, string message = null)

GreaterOrEqual

static void GreaterOrEqual<T>(T value, T threshold, string message = null, string valueExpression = null, string thresholdExpression = null)

The specified value must not be less than the specified lower bound (assert: value >= threshold)

static void GreaterOrEqual(TimeSpan value, TimeSpan threshold, string message = null, string valueExpression = null, string thresholdExpression = null)

The specified TimeSpan must not be less than the specified lower bound (assert: value >= threshold)

GreaterThan

static void GreaterThan<T>(T value, T threshold, string message = null, string valueExpression = null, string thresholdExpression = null)

The specified value must not be less than or equal to the specified lower bound (assert: value > threshold)

static void GreaterThan(TimeSpan value, TimeSpan threshold, string message = null, string valueExpression = null, string thresholdExpression = null)

The specified value must not be less than or equal to the specified lower bound (assert: value > threshold)

Invariant

static void Invariant(bool condition, string userMessage = null, string conditionText = null)

Test that an invariant is met.

  • condition — Condition that should never be false
  • userMessage — Message that describes the failed assertion (optional)
  • conditionText — Text of the condition (optional, injected by the compiler)

No-op if is true. Otherwise, throws a ContractException, after attempting to breakpoint (if a debugger is attached)

LessOrEqual

static void LessOrEqual<T>(T value, T threshold, string message = null, string valueExpression = null, string thresholdExpression = null)

The specified value must not be greater than the specified upper bound (assert: value <= threshold)

LessThan

static void LessThan<T>(T value, T threshold, string message = null, string valueExpression = null, string thresholdExpression = null)

The specified value must not be greater than or equal to the specified upper bound (assert: value < threshold)

Multiple

static void Multiple<T>(T value, T multiple, string message = null, string paramName = null)

NotEmpty

static void NotEmpty<T>(Span<T> value, string message = null, string paramName = null)

The specified span must not be empty (assert: value.Length != 0)

static void NotEmpty<T>(ReadOnlySpan<T> value, string message = null, string paramName = null)

The specified span must not be empty (assert: value.Length != 0)

static void NotEmpty<T>(Memory<T> value, string message = null, string paramName = null)

The specified memory must not be empty (assert: value.Length != 0)

static void NotEmpty<T>(ReadOnlyMemory<T> value, string message = null, string paramName = null)

The specified memory must not be empty (assert: value.Length != 0)

NotEqualTo

static void NotEqualTo(int value, int forbidden, string message = null, string paramName = null)

The specified value must not equal to the specified constant (assert: value != forbidden)

static void NotEqualTo(long value, long forbidden, string message = null, string paramName = null)

The specified value must not equal to the specified constant (assert: value != forbidden)

static void NotEqualTo(uint value, uint forbidden, string message = null, string paramName = null)

The specified value must not equal to the specified constant (assert: value != forbidden)

static void NotEqualTo(ulong value, ulong forbidden, string message = null, string paramName = null)

The specified value must not equal to the specified constant (assert: value != forbidden)

static void NotEqualTo(string value, string forbidden, string message = null, string paramName = null)

The specified value must not equal to the specified constant (assert: value != forbidden)

static void NotEqualTo<T>(T value, T forbidden, string message = null, string paramName = null)

The specified value must not equal to the specified constant (assert: value != forbidden)

NotNull

static void NotNull(object value, string message = null, string paramName = null)

The specified instance must not be null (assert: value != null)

Note that, even if value is a Value Type, the JIT will optimize the method away, and no boxing should occur

NotNullAllowStructs

static void NotNullAllowStructs<TValue>(TValue value, string message = null, string paramName = null)

The specified instance must not be null (assert: value != null)

This method allow structs (that can never be null)

OBSOLETE: please call NotNull.

NotNullOrEmpty

static void NotNullOrEmpty(string value, string message = null, string paramName = null)

The specified string must not be null or empty (assert: value != null && value.Length != 0)

static void NotNullOrEmpty<T>(T[] value, string message = null, string paramName = null)

The specified array must not be null or empty (assert: value != null && value.Count != 0)

static void NotNullOrEmpty<T>(ICollection<T> value, string message = null, string paramName = null)

The specified collection must not be null or empty (assert: value != null && value.Count != 0)

static void NotNullOrEmpty(Slice buffer, string message = null, string paramName = null)

The specified buffer must not be null or empty (assert: buffer.Array != null && buffer.Count != 0)

static void NotNullOrEmpty<T>(ArraySegment<T> buffer, string message = null, string paramName = null)

The specified buffer must not be null or empty (assert: buffer.Array != null && buffer.Count != 0)

NotNullOrWhiteSpace

static void NotNullOrWhiteSpace(string value, string message = null, string paramName = null)

The specified string must not be null, empty or contain only whitespaces (assert: value != null && value.Length != 0)

PointerNotNull

static void PointerNotNull(Void* pointer, string message = null, string paramName = null)

The specified pointer must not be null (assert: pointer != null)

Positive

static void Positive(int value, string message = null, string paramName = null)

The specified value must not be a negative number (assert: value >= 0)

static void Positive(long value, string message = null, string paramName = null)

The specified value must not be a negative number (assert: value >= 0)

static void Positive(double value, string message = null, string paramName = null)

The specified value must not be a negative number (assert: value >= 0)

static void Positive(float value, string message = null, string paramName = null)

The specified value must not be a negative number (assert: value >= 0)

PowerOfTwo

static void PowerOfTwo(int value, string message = null, string paramName = null)

The specified value must be a power of two (assert: NextPowerOfTwo(value) == value)

static void PowerOfTwo(uint value, string message = null, string paramName = null)

The specified value must be a power of two (assert: NextPowerOfTwo(value) == value)

static void PowerOfTwo(long value, string message = null, string paramName = null)

The specified value must be a power of two (assert: NextPowerOfTwo(value) == value)

static void PowerOfTwo(ulong value, string message = null, string paramName = null)

The specified value must be a power of two (assert: NextPowerOfTwo(value) == value)

Requires

static void Requires(bool condition, string userMessage = null, string conditionText = null)

Test if a pre-condition is true, at the start of a method.

  • condition — Condition that should never be false
  • userMessage — Message that describes the failed assertion (optional)
  • conditionText — Text of the condition (optional, injected by the compiler)

No-op if is true. Otherwise, throws a ContractException, after attempting to breakpoint (if a debugger is attached)

ThrowArgumentExpected

static void ThrowArgumentExpected<T>(string paramName, T expected, string message = null)

ThrowArgumentForbidden

static void ThrowArgumentForbidden<T>(string paramName, T forbidden, string message = null)

ThrowArgumentNotGreaterOrEqual

static void ThrowArgumentNotGreaterOrEqual<T>(T value, string valueExpression, string thresholdExpression, bool zero, string message = null)

ThrowArgumentNotGreaterThan

static void ThrowArgumentNotGreaterThan<T>(T value, string valueExpression, string thresholdExpression, bool zero, string message = null)

ThrowArgumentNotLessOrEqual

static void ThrowArgumentNotLessOrEqual<T>(T value, string valueExpression, string thresholdExpression, string message = null)

ThrowArgumentNotLessThan

static void ThrowArgumentNotLessThan<T>(T value, string valueExpression, string thresholdExpression, string message = null)

ThrowArgumentNotNonNegative

static void ThrowArgumentNotNonNegative(string paramName, string message = null)

ThrowArgumentNotPositive

static void ThrowArgumentNotPositive(string paramName, string message = null)

ThrowArgumentNull

static void ThrowArgumentNull(string paramName, string message = null)

ThrowArrayNullOrEmpty

static void ThrowArrayNullOrEmpty(object collection, string paramName, string message = null)

ThrowBufferEmpty

static void ThrowBufferEmpty(string paramName, string message = null)

ThrowBufferNull

static void ThrowBufferNull(string paramName, string message = null)

ThrowBufferNullOrEmpty

static void ThrowBufferNullOrEmpty(object array, string paramName, string message = null)

ThrowBufferTooSmall

static void ThrowBufferTooSmall(string paramName, string message = null)

ThrowCollectionNullOrEmpty

static void ThrowCollectionNullOrEmpty(object collection, string paramName, string message = null)

ThrowStringNullOrEmpty

static void ThrowStringNullOrEmpty(string value, string paramName, string message = null)

ThrowStringNullOrWhiteSpace

static void ThrowStringNullOrWhiteSpace(string value, string paramName, string message = null)

ValueNotNull

static T ValueNotNull<T>(T value, string message = null)

The specified value cannot be null (assert: value != null)

Returns: Passed value, or throws an exception if it was null

This method is intended for use in single-line property setters

private string m_fooThatIsNeverNull;
public string Foo
{
    get => return m_fooThatIsNeverNull;
    set => m_fooThatIsNeverNull = Contract.ValueNotNull(value, "Foo cannot be set to null");
}

Events

ContractFailed

static event EventHandler<ContractFailedEventArgs> ContractFailed

Allows a managed application environment such as an interactive interpreter (IronPython) to be notified of contract failures and potentially "handle" them, either by throwing a particular exception type, etc. If any of the event handlers sets the Cancel flag in the ContractFailedEventArgs, then the Contract class will not pop up an assert dialog box or trigger escalation policy. Hooking this event requires full trust, because it will inform you of bugs in the appdomain and because the event handler could allow you to continue execution.