Contract.Requires Method

Definition

Specifies a precondition contract for an enclosing method or property.

Overloads

Requires(Boolean)

Specifies a precondition contract for the enclosing method or property.

Requires(Boolean, String)

Specifies a precondition contract for the enclosing method or property, and displays a message if the condition for the contract fails.

Requires<TException>(Boolean)

Specifies a precondition contract for the enclosing method or property, and throws an exception if the condition for the contract fails.

Requires<TException>(Boolean, String)

Specifies a precondition contract for the enclosing method or property, and throws an exception with the provided message if the condition for the contract fails.

Requires(Boolean)

Specifies a precondition contract for the enclosing method or property.

public:
 static void Requires(bool condition);
[System.Diagnostics.Conditional("CONTRACTS_FULL")]
public static void Requires (bool condition);
static member Requires : bool -> unit
Public Shared Sub Requires (condition As Boolean)

Parameters

condition
Boolean

The conditional expression to test.

Attributes

Remarks

  • This method call must be at the beginning of a method or property, before any other code.

  • This contract is exposed to clients; therefore, it must only reference members that are at least as visible as the enclosing method.

  • Use this method instead of the Contract.Requires<TException>(Boolean) method when backward compatibility does not force you to throw a particular exception.

Requires(Boolean, String)

Specifies a precondition contract for the enclosing method or property, and displays a message if the condition for the contract fails.

public:
 static void Requires(bool condition, System::String ^ userMessage);
[System.Diagnostics.Conditional("CONTRACTS_FULL")]
public static void Requires (bool condition, string userMessage);
static member Requires : bool * string -> unit
Public Shared Sub Requires (condition As Boolean, userMessage As String)

Parameters

condition
Boolean

The conditional expression to test.

userMessage
String

The message to display if the condition is false.

Attributes

Remarks

  • This method call must be at the beginning of a method or property, before any other code.

  • This contract is exposed to clients; therefore, it must only reference members that are at least as visible as the enclosing method.

  • Use this method instead of the Contract.Requires<TException>(Boolean, String) method when backward compatibility does not force you to throw a particular exception.

Requires<TException>(Boolean)

Specifies a precondition contract for the enclosing method or property, and throws an exception if the condition for the contract fails.

public:
generic <typename TException>
 where TException : Exception static void Requires(bool condition);
public static void Requires<TException> (bool condition) where TException : Exception;
static member Requires : bool -> unit (requires 'Exception :> Exception)
Public Shared Sub Requires(Of TException As Exception) (condition As Boolean)

Type Parameters

TException

The exception to throw if the condition is false.

Parameters

condition
Boolean

The conditional expression to test.

Remarks

Important

You must turn on run-time checking to use the Requires method. If run-time checking is turned off, the process will be terminated. To obtain the tools for runtime checking, see Code Contracts on the MSDN DevLabs Web site.

  • This method call must be at the beginning of a method or property, before any other code.

  • This contract is exposed to clients; therefore, it must only reference members that are at least as visible as the enclosing method.

  • Use this method instead of the Contract.Requires(Boolean) method when you want to throw an exception if the precondition fails.

Requires<TException>(Boolean, String)

Specifies a precondition contract for the enclosing method or property, and throws an exception with the provided message if the condition for the contract fails.

public:
generic <typename TException>
 where TException : Exception static void Requires(bool condition, System::String ^ userMessage);
public static void Requires<TException> (bool condition, string userMessage) where TException : Exception;
static member Requires : bool * string -> unit (requires 'Exception :> Exception)
Public Shared Sub Requires(Of TException As Exception) (condition As Boolean, userMessage As String)

Type Parameters

TException

The exception to throw if the condition is false.

Parameters

condition
Boolean

The conditional expression to test.

userMessage
String

The message to display if the condition is false.

Remarks

  • Important

    You must turn on run-time checking to use the Requires method. If run-time checking is turned off, the process will be terminated. To obtain the tools for runtime checking, see Code Contracts on the MSDN DevLabs Web site.

    This method call must be at the beginning of a method or property, before any other code.

  • This contract is exposed to clients; therefore, it must only reference members that are at least as visible as the enclosing method.

  • Use this method instead of the Contract.Requires(Boolean, String) method when you want to throw an exception if the precondition fails.

Applies to