Why Type.Equals (t1, t2) and not an equality operator?

Why use Type.Equals(t1, t2)to define equivalent types, and not for the equality operator (for example, for VB.NET, t1 = t2)?

It seems incompatible with other parts of the .NET API.

Example in VB.NET:

If GetType(String) = GetType(String) Then Debug.Print("The same, of course") End If

causes compile time error " Operator '=' is not defined for types 'System.Type' and 'System.Type'."

+3
source share
4 answers

this, VB , . Type.Equals(t1, t2) . t1 t2 , , , #, . is IsInstanceOf, .

Typeof a Is Boolean

a.GetType().IsAssignableFrom( b.GetType() )
+3

, , . ?

docs System.Type.Equals() , UnderlyingSystemType, , .

, ... , " " , - BCL, .

+2

Reflector, , Type.Equals(t1, t2) , t1 = t2. ( Type.Equals, Object.Equals).

# T1 == T2 .

0

VB.NET Is - , . , Type.Equals , . .

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim X As New TestObject
        Dim Y As New TestObject

        If X Is Y Then MsgBox("The Same 1")
        If Type.Equals(X, Y) Then MsgBox("The Same 2")

        X = Y
        If X Is Y Then MsgBox("The Same 3")

        If Type.Equals(X, Y) Then MsgBox("The Same 4")
    End Sub
End Class

Public Class TestObject
    Public Value As Double
End Class

- 'equals' BASIC. VB4 IS, , , .

I suggest you search google and usenet for comments by Paul Wicks about why some individual BASIC idioms were ported and why others were not. I believe that in this case it would be possible to avoid confusion, since VB.NET introduced

ObjectA = ObjectC ', which forces ObjectA to reference the same objects that ObjectC refers to.

While in VB6, ObjectA = ObjectC was set

For the same reason, when objects were introduced into VB4 IS and Set, they were used to process the object instead of overloading equal.

Ultimately, these quirks became part of the Basic Coding Method.

0
source

Source: https://habr.com/ru/post/1699560/


All Articles