I have encountered a problem while using Invariants with Code Contracts. I want to define an Invariant within my abstract class but it is simply ignored. The code below shows my interface and the abstract class.
[ContractClass(typeof(IPointContract))]
interface IPoint
{
int X { get; }
int Y { get; }
}
[ContractClassFor(typeof(IPoint))]
abstract class IPointContract : IPoint
{
public int X
{
get { return 0; }
}
public int Y
{
get { return 0; }
}
[ContractInvariantMethod]
private void PointInvariant()
{
Contract.Invariant(X > Y);
}
}
Afterwards, I implement this interface within my Point class and create an object from it. This should at least fail during runtime.
class Point : IPoint
{
public Point(int X, int Y)
{
this._x = X;
this._y = Y;
}
private int _x;
public int X
{
get { return _x; }
}
private int _y;
public int Y
{
get { return _y; }
}
}
class Program
{
static void Main(string[] args)
{
Point p = new Point(1, 2);
}
}
When I move the Invariant to the Point-Class, it works fine. All other pre- or post conditions are working fine too.
Is it not possible to have Invariants within an abstract class or am I doing it wrong?