I am trying to add validation for my type at compilation time using Contract but unfortunately it do not work. For example code below do not give compiler error while compiling project. Should I enable something?
Contract.Assert(false, "Invalid state!");
Yes, you need to enable the static checking. Go into the project properties and visit the "Code Contracts" tab. Of course you'll need to have installed the static checker extension first, which IIRC is only available for Visual Studio Ultimate. See the user documentation for details of the exact options available.
Note that this happens after the initial compilation phase, so sometimes you'll see a delay between the first part of the build and the results of the static checker. (It's not really a "compiler" error - it's a "contract checking" error.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With