Imports System
Imports System.Linq
Public Module ArrayHashHelper
''' <summary>
''' Computes a hash code for an integer array.
''' Arrays with the same values in the same order
''' will produce the same hash code.
''' </summary>
''' <param name="values">The integer array to hash.</param>
''' <returns>A hash code representing the array contents.</returns>
Public Function GetArrayHashCode(values As Integer()) As Integer
' Handle null input.
If values Is Nothing Then
Return 0
End If
' HashCode provides a high-quality hash implementation.
Dim hash As New HashCode()
' Add each element in order.
' Order matters: [1,2,3] != [3,2,1].
For Each value As Integer In values
hash.Add(value)
Next
Return hash.ToHashCode()
End Function
Public Sub Main(args As String())
Dim numbers As Integer() = {1, 2, 3, 4, 5}
Dim hashCode As Integer = ArrayHashHelper.GetArrayHashCode(numbers)
Console.WriteLine(hashCode)
Dim array1 As Integer() = {1, 2, 3}
Dim array2 As Integer() = {1, 2, 3}
Dim hash1 As Integer = ArrayHashHelper.GetArrayHashCode(array1)
Dim hash2 As Integer = ArrayHashHelper.GetArrayHashCode(array2)
If hash1 = hash2 Then
Console.WriteLine("Arrays may be equal.")
End If
If hash1 = hash2 AndAlso array1.SequenceEqual(array2) Then
Console.WriteLine("Arrays are equal.")
End If
End Sub
End Module
' run:
'
' 181771288
' Arrays may be equal.
' Arrays are equal.
'