Code Contracts: IEnumerator <T> .GetEnumerator () weird inherited contract?
I use Code Contracts with the VS2010 Extension Code Contract Editor. I have a class that implements an interface IEnumerable<T>, and I realized the iterator block for the method GetEnumerator(). Above it, I see the following inherited contract:
![ensures result! = null ensures result.Model == ((IEnumerable) this) .Model [Pure] public IEnumerator (of IBaseMessage) GetEnumerator () {](/img/0fa358ae40cb2cb55972799515aa9bb5.png)
I understand the first and third requirement of the contract - GetEnumerator()it should never return null, and it should never cause a side effect. But what does the second requirement of the contract mean? What is this Modelproperty IEnumerator<T>and IEnumerable?
EDIT: As Damien_The_Unbeliever noted in his comment, the contract for IEnumerable<T>and IEnumerator<T>is in a separate file - the assembly of contracts. Using Reflector , when disassembling the contract of these two interfaces (full code here ), you can see the following:
[return: Fresh]
[Escapes(true, false), Pure, GlobalAccess(false)]
public IEnumerator GetEnumerator()
{
IEnumerator enumerator;
Contract.Ensures((bool) (Contract.Result<IEnumerator>() != null), null, "Contract.Result<IEnumerator>() != null");
Contract.Ensures((bool) (Contract.Result<IEnumerator>().Model == this.Model), null, "Contract.Result<IEnumerator>().Model == this.Model");
Contract.Ensures((bool) (Contract.Result<IEnumerator>().CurrentIndex == -1), null, "Contract.Result<IEnumerator>().CurrentIndex == -1");
return enumerator;
}
Interestingly, there GetEnumerator()is an additional contract that is not displayed by the editor extension:
Contract.Result<IEnumerator>().CurrentIndex == -1
And some additional secrets (such as attributes Fresh, Escapesand GlobalAccess).