public int ProcessInvoice(EdifactInvoice invoice) > { > if (invoice == null) > throw new NullReferenceException("Invoice cannot be null"); > > //Elided > return 5; > }Using code contracts you can refactor this to:
public int ProcessInvoice(EdifactInvoice invoice) > { > Contract.Requires<NullReferenceException>(invoice != null); > Contract.Ensures(Contract.Result<int>() > 0); > //Elided > return 5; > }The 'Contract.Requires' serves the same purpose as the original null check at runtime - but with versions higher than VS Pro will *also* catch any existing call to the method which might fail to fullfill the 'contract at compile time. The Contract.Ensures line will, in this non-realistic example, also throw an exception if the return value does not meet the condition. A neat thing with the latter is that it will do this even if there are multiple return paths.....