coderushforroslyn-115562-coding-assistance-code-providers-contract-providers-exit-method-contract.md
This Code Provider adds the guard precondition to method parameter(s) or a variable assignment. The precondition ensures the passed or assigned values are not empty and exits the method in case they are.
Available in the following cases.
Note
This Code Provider may be unavailable for variables of a non-nullable type, for instance, Integer.
Place the caret at the beginning of a method body.
Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
Select Add Contract | Exit Method Contract from the menu.
After execution, the Code Provider adds the guard conditional against all the method parameters.
public bool AddRecord(string name, object data) {
if (string.IsNullOrEmpty(name) || data == null)
return false;
//...
return true;
}
Public Function AddRecord(ByVal name As String, ByVal data As Object) As Boolean
If String.IsNullOrEmpty(name) OrElse data Is Nothing Then
Return False
End If
'...
Return True
End Function
You can ask CodeRush to check arrays and collections length in code contracts. Refer to the Code Actions Settings topic for more information.
See Also