views:

73

answers:

3

Please tell me what those functions do.

+1  A: 

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.

jjnguy
+1  A: 

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.

Phil Gilmore
if it only for checking even we can use if conditions but what is the need of these type of methods ....
Krishna
U mean to say like these functions checks conditions, its throws an exception....
Krishna
By default, the user will see a unique exception dialog. It is an abort/retry/ignore dialog. But it is an exception that can be trapped.
Phil Gilmore
The point to this really isn't for flow control. If you want to check a condition, use IF control blocks and normal exception handling. Assertions are there to prove that the PROGRAM is doing what it should. It's to catch bugs. You say, "This method should never return a value outside this range". If it were ever to happen, it would mean you have a bug. These assertions are to catch such bugs. In a perfect world, the user could never do anything that would violate these. They should be used to check assumptions about your code, not so much your data.
Phil Gilmore
Can u let me know even require() and Ensure() methods returns an exception dialog when condition fails... can u brief with any simple example reg this Assert() and Require() and Ensure() methods....Even is there are any other such type of methods where it uses check.<methodname>
Krishna
A: 

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.

FuleSnabel