Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Code Contracts to work in Visual Studio 2010

I have the following code:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(SqrtRoot(0));
        Console.WriteLine(SqrtRoot(10));
        Console.WriteLine(SqrtRoot(-10));
        Console.ReadKey();
    }

    public static int SqrtRoot(int i)
    {
        Contract.Requires(i >= 0);
        return (int)Math.Sqrt(i);
    }
}

I am running it in debug mode, and it should fire some kind of error in the last line

Console.WriteLine(SqrtRoot(-10));

altough, for some reason, it doesn't. It seems to ignore the Contract.Requires() call. Should I set up something when trying to use Code Contracts?

I'm using Visual Studio 2010 RC.

Thanks

like image 594
devoured elysium Avatar asked Feb 14 '10 22:02

devoured elysium


1 Answers

You need to install the Visual Studio integration. While the CodeContracts library itself is part of .NET 4, your code needs to be rewritten by the Code Contracts rewriter (ccrewrite) to actually use the library properly.

Download the installer from the DevLabs site.

like image 138
porges Avatar answered Oct 10 '22 02:10

porges