我有一个非常简单的类使用.NET代码合同:
public class ContractSquareRoot
{
/// <summary>
/// Makes your life much easier by calling Math.Sqrt for you. Ain't that peachy.
/// </summary>
/// <param name="value">The value to calculate the square root from. No negatives!</param>
/// <returns>The square root of the given value. Obviously always > 0.</returns>
public double CalculateSquareRoot(double value)
{
Contract.Requires<ArgumentException>(0 <= value);
Contract.Ensures(0 <= Contract.Result<double>());
double squareRoot = Math.Sqrt(value);
return squareRoot;
}
}
Run Code Online (Sandbox Code Playgroud)
当我用负值调用方法时,我希望静态代码分析能够警告我.
class Program
{
static void Main(string[] args)
{
var barMansSquareroot = new ContractSquareRoot(); …Run Code Online (Sandbox Code Playgroud)