0

I'm testing some code using System.Diagnostics.Contracts.Contract.Requires and System.Diagnostics.Contracts.Contract.Ensures and while it compiles fine, it appears to do absolutely nothing. I passed parameters to 'Requires' that obviously make the condition false and the code will run as if the 'Requires' call wasn't even there.

I'm using VS 2017 and targeting .NET Framework 4.5.

Example:

private static void one(int i)
{
    System.Diagnostics.Contracts.Contract.Requires(i > 0);
}

private static void two(int i)
{
    one(0);  //the call is being made, but 'Requires' not complaining
}
Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28
  • Are you running your code in debug or release mode? – FlyingFoX Jul 23 '19 at 22:23
  • I'm running in debug mode. – Dave Doknjas Jul 23 '19 at 22:24
  • 1
    Have you turned on run time checking as stated in the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.contracts.contract.requires?view=netframework-4.8)? – FlyingFoX Jul 23 '19 at 22:28
  • @FlyingFoX: No, but curious about why it compiles then? – Dave Doknjas Jul 23 '19 at 22:28
  • @FlyingFoX: Thanks for your help. I think it's sloppy on Microsoft's (or open source anonymous coder?) part to provide this feature with no exception to indicate that it can't possibly work unless these other switches are turned on. Should definitely not compile fine for my case. – Dave Doknjas Jul 23 '19 at 22:32
  • Yeah these requirements are not very prominent in the documentation. – FlyingFoX Jul 23 '19 at 22:33
  • @FlyingFoX: That's not the part that annoys me so much - I was rushing the testing without reading carefully enough (as I often do), but allowing something to compile in this case is nonsense. Or even more to the point, why doesn't the framework contain what is needed to support these framework methods? – Dave Doknjas Jul 23 '19 at 22:36
  • I really can't answer your questions. I have never used these Contracts myself and have absolutely no idea why they work like they do. Please look into their documentation and it may get more clear. – FlyingFoX Jul 23 '19 at 22:43

0 Answers0