Recently I’ve noticed that when we’ve been doing string comparisons in our .NET code we have been safeguarding against null references. This check seems unnecessary in VB.NET because it overloads the equality operator and performs this null ref check for us. Do you all agree ?
See examples below:
Dim myStringVariable As String
' Unnecessary in VB.NET
If myStringVariable Is Nothing OrElse myStringVariable = "" Then
End If
' Unnecessary in VB.NET
If String.IsNullOrEmpty(myStringVariable) Then
End If
' This will work
If myStringVariable = "" Then
End If
' Also acceptable if you don't like using hard coded constants
If myStringVariable = String.Empty Then
End If
Specifically for equality tests you may be right. However, equality tests are not the only thing you do with strings.
I have not done much VB.Net, but I strongly suspect that
will blow up if myStringVariable is null, er… Nothing.