Difference in Visual Basic between 'Not a = b' and 'a <> b'

The title really says it all. In VB.NET there is a difference between these assertions when checking for value equality

Dim notEqualsCompare as Boolean = Not a = b
Dim angleBracCompare as Boolean = a <> b

      

I usually use "Not a = b" because it reads better. I read the "<>" operator as "less or more", which does make sense for numeric values, but from my brief testing, they behave the same.

Is there ever a situation where it is preferable?

+3


source share


2 answers


In practice, they should always be functionally equivalent.

As mentioned in one of the other answers, it is Not a = b

logically two separate operations. You can trust that the compiler will optimize this for you, but regardless, most programmers will agree that if there is an operator in the language that will do what you want, you should usually use that language feature rather than use multiple operations to accomplish the same result. If you do not like this language, please choose another language. But if you are going to use VB, you better get used to it and use its syntax.

Technically, however, it's worth mentioning that they are not necessarily the same. All three of the following lines may technically have different results:

result = Not a = b
result = a <> b
result = Not a.Equals(b)

      



The reason they might be different is because VB allows you to override operator =

(equality test, not assignment), operator <>

, operator, Not

and method Equals

. So while it would be terrible to override these things and provide different functionality for each, it is technically possible. For obvious reasons, the "official" guidelines from Microsoft recommend that they always work consistently with each other.

Consider:

Public Sub Main()
    Dim a As New Crazy()
    Dim b As New Crazy()
    Console.WriteLine(a = b)            ' False
    Console.WriteLine(a.Equals(b))      ' True
    Console.WriteLine(a <> b)           ' False
    Console.WriteLine(Not a)            ' "Opposite"
    Console.WriteLine(Not a = b)        ' True
    Console.WriteLine(Not a.Equals(b))  ' False
End Sub

Private Class Crazy
    Public Shared Operator <>(x As Crazy, y As Crazy) As Boolean
        Return False
    End Operator

    Public Shared Operator =(x As Crazy, y As Crazy) As Boolean
        Return False
    End Operator

    Public Overrides Function Equals(obj As Object) As Boolean
        Return True
    End Function

    Public Shared Operator Not(value As Crazy) As String
        Return "Opposite"
    End Operator
End Class

      

+1


source


They both give the same answer, but a <> b

are likely to be more efficient. The reason is that when the compiler comes up with "Not a = b", the compiler must do the comparison ( a = b

) first, then it must use clock cycles to reverse that login ( Not

). In doing so a <> b

, the compiler will only have to do the comparison and then do it. Take a look at this diagram:


a <> b    ' One Operation

      





c = a = b      ' One Operation
Not c          ' Another Operation

      

0


source







All Articles