我查看了MSDN,并自己摸索改了一整天~有结果了
以下是我改好的代码,大家可以修改细节来达到你们所要的类型结构.
我虽然CSDN等级不高,但我有共享精神.不是机密的东西,我希望共享给大家,让国内小虾们的水平上升一些些.
Public Class ProductA
Public Name As String
Public Code As Integer
Overloads Function SequenceEqual(obj As ProductA) As Boolean
'检查对象是否是相同的对象。
If Object.ReferenceEquals(Me, obj) Then Return True
'检查类的属性是否相等。
Return Me IsNot Nothing And obj IsNot Nothing And Me.Code.Equals(obj.Code) And Me.Name.Equals(obj.Name)
End Function
Overloads Function GetHashCode() As Integer
'获取名称字段的哈希码,如果它不为空。
Dim hashProductName As Integer = IIf(Me.Name Is Nothing, 0, Me.Name.GetHashCode())
'获取代码字段的哈希码。
Dim hashProductCode As Integer = Me.Code.GetHashCode()
'异或计算类的哈希码。
Return hashProductName Xor hashProductCode
End Function
End Class
Sub SequenceEqualEx1()
Dim storeA() As ProductA =
{New ProductA With {.Name = "apple", .Code = 9},
New ProductA With {.Name = "orange", .Code = 4}}
Dim storeB() As ProductA =
{New ProductA With {.Name = "apple", .Code = 9},
New ProductA With {.Name = "orange", .Code = 0}}
Dim equalAB As Boolean = storeA(0).SequenceEqual(storeB(0))
Dim equalBC As Boolean = storeA(1).SequenceEqual(storeB(1))
Dim A0 As Integer = storeA(0).GetHashCode()
Dim B0 As Integer = storeB(0).GetHashCode()
Dim A1 As Integer = storeA(1).GetHashCode()
Dim B1 As Integer = storeB(1).GetHashCode()
MsgBox("A0等于B0吗? " & equalAB & vbCrLf &
"A1等于B1吗? " & equalBC & vbCrLf &
"HashA0: " & A0 & vbCrLf &
"HashB0: " & B0 & vbCrLf &
"HashA1: " & A1 & vbCrLf &
"HashB1: " & B1)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SequenceEqualEx1()
End Sub