user14691414
user14691414

Reputation: 31

Code contract does not work in Visual Studio 2019

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.

Upvotes: 3

Views: 972

Answers (1)

Athanasios Kataras
Athanasios Kataras

Reputation: 26362

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.

Upvotes: 1

Related Questions