Contract.Debug

Namespace: SnowBank.Diagnostics.Contracts · class

Contracts that are only evaluated in Debug builds

Methods

Assert

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

[DEBUG ONLY] 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 or if running a Release build. Otherwise, throws a ContractException, after attempting to breakpoint (if a debugger is attached).

Ensures

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

[DEBUG ONLY] 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 or if running a Release build. Otherwise, throws a ContractException, after attempting to breakpoint (if a debugger is attached).

Fail

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

[DEBUG ONLY] 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)

Invariant

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

[DEBUG ONLY] 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 or if running a Release build. Otherwise, throws a ContractException, after attempting to breakpoint (if a debugger is attached).

Requires

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

[DEBUG ONLY] 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 or if running a Release build. Otherwise, throws a ContractException, after attempting to breakpoint (if a debugger is attached).