Downloading, installing, and integrating code contracts into Visual Studio

Before you can use code contracts in your applications, you need to download and install them. The easiest way of doing this is via extensions and updates. After the installation is complete, you will need to define a few settings for the code contracts to start functioning against the code they are implemented in. Let's have a look at the following recipe.

Getting ready

First, we will create a new class and add it to our Visual Studio project. We will then get the Code Contracts installer and install it for our project.

How to do it…

  1. Create a new class by right-clicking on your solution and selecting Add and then New Project from the context menu:
    How to do it…
  2. From the Add New Project dialog screen, select Class Library from the installed templates and call your class Chapter8:
    How to do it…
  3. Your new class library will be added to your solution with a default name of Class1.cs, which we renamed to Recipes.cs in order to distinguish the code properly. You can, however, rename your class to whatever you like.
  4. To rename your class, simply click on the class name in Solution Explorer and select Rename from the context menu:
    How to do it…
  5. Visual Studio will ask you to confirm a rename of all references to the code element Class1 in the project. Just click on Yes:
    How to do it…
  6. Next, click on the Tools menu and select Extensions and Updates…:
    How to do it…
  7. You will see the Extensions and Updates window appear. Be sure to click on the Visual Studio Gallery on the left-hand side and type Code Contracts as the search term. If you have not got the Code Contracts installer, you will see a download button appear on the Code Contracts for .NET result. Click on it to download and install code contracts:
    How to do it…
  8. After code contracts have been installed, you might need to restart Visual Studio. After doing this, right-click on the Chapter8 project and select Properties from the context menu:
    How to do it…
  9. You will notice that a new Code Contracts tab has been added to the properties page for your Chapter8 project. Click on this tab and make sure that Perform Runtime Contract Checking is checked. Then, save your changes and close the properties page:
    How to do it…
  10. Finally, add a reference to your Chapter8 project in the console application created earlier. Do this by expanding your console application project and right-clicking on the References item. Select Add Reference from the context menu:
    How to do it…
  11. Make sure that you have selected Chapter8 in the project references section and click on OK:
    How to do it…

How it works…

You have now installed and configured the minimum requirements to enable code contracts in your Chapter8 class. You can now go ahead and build your solution to make sure that everything builds successfully.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.226.121