Created
October 29, 2015 23:42
-
-
Save liamzebedee/1ede475edde84d9ffbcb to your computer and use it in GitHub Desktop.
Code Contracts cheatsheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Contract.Ensures(Contract.Result<T>() != null); | |
| Contract.Requires(T != null); | |
| Contract.ForAll(Collection, (element) => element != null) | |
| Contract.Requires(Contract.ForAll(Collection, (element) => element != null)); | |
| Contract.Invariant(condition); | |
| [ContractInvariantMethod] | |
| private void ObjectInvariant () {} | |
| Contract.Ensures(Contract.ForAll(this.Entities, (entity) => entity != null)); | |
| [ContractClass(typeof(FooContract))] | |
| abstract class Foo { | |
| public abstract int Count { | |
| get; | |
| } | |
| public abstract void Put(int value); | |
| } | |
| [ContractClassFor(typeof(Foo))] | |
| abstract class FooContract: Foo { | |
| public override int Count { | |
| get { | |
| Contract.Ensures(0 <= Contract.Result < int > ()); | |
| return default (int); // dummy return | |
| } | |
| } | |
| public override void Put(int value) { | |
| Contract.Requires(0 <= value); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment