Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the. NET 4. 0 code contract component

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to understand. NET 4.0 code contract components, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

The code contract component is an important supplement to .NET, and we will provide more details this time.

If you want to use the 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/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).

Contracts include several types: pre-conditions (Preconditions), post-conditions (Postconditions), object invariants (Object Invariants), assertions (Assertions), assumptions (Assumptions), quantifiers (Quantifiers), interface contracts (Interface Contracts), and abstract method contracts (Abstract Method Contracts).

The precondition is defined using Contract.Requires (), and if the symbol (Symbol) CONTRACTS_FULL or CONTRACTS_PRECONDITIONS is used at compile time, then the compilation result is included in the IL. For example:

Contract.Requires (x! = null)

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

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

If the conditions specified by Contract.Requires () are not met, Debug.Assert (false) is called, and then Environment.FailFast () is called. If you want the assembly to contain preconditions no matter which symbol you use at compile time, you can use Contract.RequiresAlways ().

When the method ends, the post-condition represents the contract whose result needs to be satisfied. 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 the condition seems to be specified before the result is returned, it is actually validated after the result is returned and before the caller gets the result.

Object invariants specify conditions for each instance.

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

For other types of contracts, assertions are expressed as Contract.Assert () and assumptions as Contract.Assume (). A failed Assert () calls Debug.Assert (false). The assumption is similar to the runtime assertion, except in the way it is statically checked. It is assumed that it is used to specify the condition that "expectation" should meet, which cannot be verified by the compiler due to some limitations.

The interface contract specifies conditions for the interface. They are used on separate classes associated with an interface because interface methods can only be declared and cannot have a method body. The same is true for abstract method contracts.

The following is a class that uses contracts:

Net contract class code example on how to understand. NET 4.0 code contract components are shared here, I hope the above content can be of some help to you, you can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report