IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Cross Ref Guide
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesC#  Print This     

Code Contract (C# and Delphi Cross Reference Guide)

By Mike Prestwood

C# versus Delphi: A side by side comparison between C# and Delphi.

 
OOP Details
 

More object oriented (OO) stuff.

Code Contract

[Other Languages] 

A.k.a. Class Contract and Design by Contracts.

A contract with a method that must be true upon calling (pre) or exiting (post). A pre-condition contract must be true when the method is called. A post-condition contract must be true when exiting. If either are not true, an error is raised. For example, you can use code contracts to check for the validity of input parameters, and results

An invariant is also a code contract which validates the state of the object required by the method.

C#:  "Code Contracts" Not Supported

Although not currently supported, there are plans for the next version of VS.Net and .Net using Requires and Ensures keywords. Look for code contracts in VS.Net 2010 and .Net 4.

If you code with VS.Net 2008 Pro or above and wish to implement contracts now, checkout the following download:

 

Syntax Example:
//Future syntax may look something like:
string SetAge(int pAge) { 
Contract.Requires(pAge>0);
}
  
string GetAge() { 
Contract.Ensures(Contract.Result() != null);
}
Delphi:  "Code Contracts" Not Supported

Delphi does not offer built-in Design by Contract features. It does offer an Assert method which tests if an expression is true. If false, an EAssertionFailed exception is raised. Although you can use Assert in a similar manor as you would use a pre-condition contract, the Delphi help clearly says Assert is a debugging tool only and to not use it in production code.

//Although not intended as a code contract feature,
//Assert is useful for debugging.
method Cyborg.Walk(pPace);
begin
Assert(pPace > 0);
Assert(pPace < 100);
  
  //Overloaded version with message.
Assert(FEnergyLevel >= 10, 'Energy level too low.');
end;












Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]