I am new to contracts I went through documentation of msdn for code contracts
https://msdn.microsoft.com/en-us/library/dd264808(v=vs.110).aspx
I picked the idea of Contract Invariants. However when I add the Invariant method It does not access fields form interface.
[ContractClass(typeof(Account_Contract))]
public interface IAccount
{
int AccountID { get; }
string AccountOwner { get; }
int GetAccountID(); // Our 'internal'unique identifier for this account
}
[ContractClassFor(typeof(IAccount))]
internal abstract class Account_Contract : IAccount
{
int IAccount.AccountID
{
get
{
Contract.Ensures(Contract.Result<int>() > 0);
return default(int);
}
}
string IAccount.AccountOwner
{
get
{
Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()));
return default(string);
}
}
int IAccount.GetAccountID()
{
Contract.Ensures(Contract.Result<int>() > 0);
return default(int);
}
}
It works fine but if I add
Contracts.Requires(AccountID > 0);
VS does not access it. I have also tried
Contracts.Requires(this.AccountID > 0);
Contracts.Requires(IAccount.AccountID > 0);
None working. What is that I'm doing wrong.