Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,700 questions

57,013 answers

573 users

How to get and use a hash code for an array of integers in VB.NET

1 Answer

0 votes
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.
' 

 



answered 3 days ago by avibootz
...