Detailed. NET 4.0 Code contract components

Source: Internet
Author: User
Tags abstract assert

The Code contract component is right. NET, this time we will provide more detailed content.

If you want to use a code contract before the. NET 4.0 release, we can reference the assembly Microsoft.Contracts.dll in the Visual Studio project, which is installed in the%programfiles%/microsoft/ In the Contracts/publicassemblies directory.. NET 4.0 includes contract components in mscorlib.dll. We can specify contract validation, which can be performed at compile-time (static) or at run time (dynamic).

A contract consists of several types: a precondition (preconditions), a post condition (postconditions), an object invariant (objects invariants), an assertion (assertions), an assumption (assumptions), Quantifiers (quantifiers), interface contracts (Interface contracts) and abstract method contracts (abstract methods contracts).

A precondition is defined by using Contract.Requires (), and if the notation (symbol) Contracts_full or contracts_preconditions is used at compile time, its compilation results are included in the IL. For example:

Contract.Requires( x ! = null );

As shown below, the precondition is usually validated as a parameter in the method body, as follows:

public Rational( int numerator, int denominator)
{  Contract.Requires(  denominator ! = 0 );
   this .numerator = numerator;
   this  .denominator = denominator;
}

If the condition specified by Contract.Requires () is not met, Debug.Assert (false) is invoked, and then the Environment.failfast () is invoked. If you want to include a precondition in your assembly regardless of which symbol is used at compile time, you can use Contract.requiresalways ().

When the method ends, the post condition represents the contract whose result needs to be met. It is specified by the Contract.ensures () method, as shown in the following example:

public int Denominator {
  get {
     Contract.Ensures(  Contract.Result() != 0 );
     return this .denominator;
    }
}

Although it seems that a condition has been specified before the result is returned, it will actually be validated before the caller gets the result after returning the result.

Object invariants specify criteria for each instance.

ContractInvariantMethod]protected void ObjectInvariant () {
Contract.  Invariant ( this .denominator ! = 0 );
  }

For other types of contracts, the assertion is expressed as contract.assert () and is assumed to be expressed as contract.assume (). A failed Assert () invokes Debug.Assert (false). The assumption is similar to the Run-time assertion, except that the static test is the way. This is assumed to specify the conditions that "expect" should conform to, and because of some limitations, the condition cannot be validated by the compiler.

Interface contracts specify conditions for an interface. They are used on separate classes that are associated with interfaces, because an interface method can only be declared, not a method body. The same is true for abstract method contracts.

The following is a class that uses a contract:

. NET Contract Class code example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.