The code contract generated by the Code Action "Add Contract" - "Throw Exceptions Contract" is incorrect. The generated code will throw an exception when the application is run/tested.
For example, given this method:
Visual BasicPublic Sub DoSomething(values() As Integer)
End Sub
Click on "values()" and select the code action "Add Contract" - "Throw Exceptions Contract". The code generated by CR is:
Visual BasicPublic Sub DoSomething(values() As Integer)
If values Is Nothing Or values.Length = 0 Then
Throw New ArgumentException($"{NameOf(values)} is nothing or empty.", NameOf(values))
End If
End Sub
If the values parameter is null (Nothing ), a NullReferenceException will be thrown when the method is run or tested. This is because VB.NET does not short-circuit the logical "OR" as C# does.
The correct VB.NET contract should be:
Visual BasicPublic Sub DoSomething(values() As Integer)
If values Is Nothing OrElse values.Length = 0 Then
Throw New ArgumentException($"{NameOf(values)} is nothing or empty.", NameOf(values))
End If
End Sub
Note the change of "Or" to "OrElse".
Hi William,
Thank you for the code sample and detailed explanation. I have reproduced the issue and we are working on it.