3

I was learning code contract but it doesn't throw me any error or anything. I am using Visual Studio 2019.

Here's the code:

using System.Diagnostics.Contracts;

namespace ConsoleApp1
{
    class Calculations
    {
        public static void Division(string name)
        {
            Contract.Requires(!string.IsNullOrEmpty(name));
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string s = null;
            Calculations.Division(s);
        }
    }
}

How is this not throwing me anything? I pretty much violated the contract when I call Division.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • if you want to use the code contract and throw an exception in your code, you can refer to the steps in the [Contract.Requires usage](https://stackoverflow.com/questions/8300815/contract-requires-usage) – Jack J Jun Nov 24 '20 at 02:14

1 Answers1

1

Most probably because of the following line in the documentation

Most methods in the contract class are conditionally compiled; that is, the compiler emits calls to these methods only when you define a special symbol, CONTRACTS_FULL, by using the #define directive. CONTRACTS_FULL lets you write contracts in your code without using #ifdef directives; you can produce different builds, some with contracts, and some without.

It is also mentioned in the documentation that once you setup and use the Code.Contracts UI, then the CONTRACTS_FULL is defined.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61