Using code contracts on abstract classes
If you use abstract classes in your code, you will know that being able to control how they are used with code contracts will result in more robust code. But how exactly can we use code contracts with abstract classes? Especially since abstract classes are supposed to contain no implementation? Well, it is definitely possible, and here is how we do it.
Getting ready
If you have not worked with abstract classes before, we advise you to first read Chapter 2, Classes and Generics, to familiarise yourself with how abstract classes are used and created.
How to do it…
Before you go on, ensure that you have added the code contracts
using
statement to the top of yourRecipes.cs
class file:using System.Diagnostics.Contracts;
Create an abstract class called
Shape
that defines two methods calledLength()
andWidth()
which each take an integer value as a parameter. Remember that abstract classes contain no implementation:public abstract class Shape { public abstract...