Skip to content

Instantly share code, notes, and snippets.

@liamzebedee
Created October 29, 2015 23:42
Show Gist options
  • Select an option

  • Save liamzebedee/1ede475edde84d9ffbcb to your computer and use it in GitHub Desktop.

Select an option

Save liamzebedee/1ede475edde84d9ffbcb to your computer and use it in GitHub Desktop.
Code Contracts cheatsheet
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