Question

Please tell me what those functions do.

Was it helpful?

Solution

These are framework-based, language-agnostic methods for defining code contracts in .NET. While some languages such as spec# and Delphi Prism have first-class language support for code contracts, this framework-based approach makes it available to all .NET languages. IronRuby, IronPython, F#, VB.NET etc.

Require() is a method-level check on ENTRY, Ensure() is a method-level check on EXIT, and Assert() is a check at whatever point it is called. That is to say that at whatever point of execution these methods apply, the condition which you pass to them will be evaluated. If that condition is not met, an error occurs.

OTHER TIPS

I googled your title and found the following:

http://www.codeproject.com/KB/cs/designbycontract.aspx

Basically, they are methods that help you write code using the "Design by Contract" methodology.

In addition to what others said Contracts can also be checked during compile time if you run the more expensive versions of Visual Studio. That way you can prove that for instance an argument is never null in your program. That also means that the check will be unnecessary and C# compiler can optimize it away.

These compile time checks can be very useful if you have very high requirement on quality and a relative small and tight codebase.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top