Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting malformed contract, in by C# code?

Visual Studio shows an error when I write this contract below.

Error 20 Malformed contract section in method '....get_Page'

Is the problem with the if block?

public int? Page
{
  get
  {
    int? result = Contract.Result<int?>();

    if (result != null)
        Contract.Ensures(result >= 0);

    return default(int?);
  }
}

EDIT:

Lasse V. Karisen has posted in comments:

How about: Contract.Ensures(result == null || result >= 0); ?

Yes Karisen, I've tried this before and it compiles. But the question remains: isn't it possible to have ifs when using contracts?

Another problem I'm having is clueless (mainly considering the example above works), involves the use of result also:

public int IndexOf(T item)
{
    Contract.Assert(item != null);
    Contract.Assert((item as IEntity).ID > 0);

    int result = Contract.Result<int>();
    Contract.Ensures(result >= -1);

    return default(int);
}
like image 998
Victor Rodrigues Avatar asked Dec 14 '22 00:12

Victor Rodrigues


1 Answers

The contract is malformed because all contract clauses MUST appear before any other code.

like image 186
bkqc Avatar answered Dec 29 '22 04:12

bkqc