Do you really need a method just to call string.IsNullOrEmpty(str) ? And since String.IsNullOrEmpty(string) already marked as [Pure] in BCL, and since the shell is extraneous, the whole problem will simply disappear if you call it directly.
If you really take this method very strongly, then one of the ways this can work with your current code is to change the contract according to your Test method:
private void Test(Form form) { Contract.Requires(Valid(form.Name)); MessageBox.Show(form.Name); } [Pure] private bool Valid(string str) { return !string.IsNullOrEmpty(str); }
Now the static analyzer should not complain.
source share