Class Contract
This is a minimal implementation of the CodeContracts API that represents 90% of our use cases. It doesn't do anything fancy and ends up being sugar for standard exception throwing.
Inherited Members
Namespace: Acoustics.Shared.Contracts
Assembly: Acoustics.Shared.dll
Syntax
public static class Contract
Methods
| Improve this Doc View SourceEnsures(Boolean, String)
Require the supplied boolean to be true, otherwise throw an exception. This is a mirror of Requires(Boolean, String) and behaves identically. If you wish to check a condition at the end of your method, move the Ensures(Boolean, String) call there.
Declaration
[ContractAnnotation("result:false => halt")]
public static void Ensures(bool result, string message = "Precondition failed")
Parameters
Type | Name | Description |
---|---|---|
Boolean | result | Whether or not the exception should be thrown. |
String | message | The message to add to the exception if the check fails. |
Ensures<T>(Boolean, String)
Require the supplied boolean to be true, otherwise throw an exception. This is a mirror of Requires<T>(Boolean, String) and behaves identically. If you wish to check a condition at the end of your method, move the Ensures<T>(Boolean, String) call there.
Declaration
[ContractAnnotation("result:false => halt")]
public static void Ensures<T>(bool result, string message = "Precondition failed")
where T : Exception
Parameters
Type | Name | Description |
---|---|---|
Boolean | result | Whether or not the exception should be thrown. |
String | message | The message to add to the exception if the check fails. |
Type Parameters
Name | Description |
---|---|
T | The type of exception to throw. |
EnsuresNotNull(Object, String, String)
Ensures the supplied value to be not null, otherwise throw a argument null exception.
Declaration
[ContractAnnotation("value:null => halt")]
public static void EnsuresNotNull(object value, string name = "", string message = "Postcondition failed - value was null")
Parameters
Type | Name | Description |
---|---|---|
Object | value | Whether or not the exception should be thrown. |
String | name | The name of the argument that was null. |
String | message | The message to add to the exception if the check fails. |
Requires(Boolean, String)
Require the supplied boolean to be true, otherwise throw an Argument
Declaration
[ContractAnnotation("result:false => halt")]
public static void Requires(bool result, string message = "Precondition failed")
Parameters
Type | Name | Description |
---|---|---|
Boolean | result | Whether or not the exception should be thrown. |
String | message | The message to add to the exception if the check fails. |
Requires<T>(Boolean, Object[])
Require the supplied boolean to be true, otherwise throw an exception.
Declaration
[ContractAnnotation("result:false => halt")]
public static void Requires<T>(bool result, params object[] args)
where T : Exception
Parameters
Type | Name | Description |
---|---|---|
Boolean | result | Whether or not the exception should be thrown. |
Object[] | args | The arguments to supply to the exception's constructor. |
Type Parameters
Name | Description |
---|---|
T | The type of exception to throw. |
Requires<T>(Boolean, String)
Require the supplied boolean to be true, otherwise throw an exception.
Declaration
[ContractAnnotation("result:false => halt")]
public static void Requires<T>(bool result, string message = "Precondition failed")
where T : Exception
Parameters
Type | Name | Description |
---|---|---|
Boolean | result | Whether or not the exception should be thrown. |
String | message | The message to add to the exception if the check fails. |
Type Parameters
Name | Description |
---|---|
T | The type of exception to throw. |
RequiresNotNull(Object, String, String)
Require the supplied value to be not null, otherwise throw a argument null exception.
Declaration
[ContractAnnotation("value:null => halt")]
public static void RequiresNotNull(object value, string name = "", string message = "Precondition failed - value was null")