Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7659435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:19:57+00:00 2026-05-31T13:19:57+00:00

I saw the question posed here: Have I implemented Equals()/GetHashCode() correctly? but my c#

  • 0

I saw the question posed here: Have I implemented Equals()/GetHashCode() correctly? but my c# is not as strong, and I am unfimiliar with IEquatable enough that I would like to see this in VB.NET if possible please.

My example code (The class will eventually use INotifyPropertyChanged when I get there):

Public Class Car
Implements ICloneable
Implements IEquatable(Of Car)

Public Property Make() As String
    Get
        Return m_Make
    End Get
    Set(ByVal value As String)
        m_Make = value
    End Set
End Property
Private m_Make As String

Public Property Model() As String
    Get
        Return m_Model
    End Get
    Set(ByVal value As String)
        m_Model = value
    End Set
End Property
Private m_Model As String

Public Function Clone() As Object Implements System.ICloneable.Clone
    Return New Car() With { _
     .Make = Me.Make, _
     .Model = Me.Model _
    }
End Function

Public Overloads Function Equals(ByVal other As Car) As Boolean Implements System.IEquatable(Of Car).Equals
    Return other.Make = Me.Make AndAlso other.Model = Me.Model
End Function 
End Class

Thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:19:58+00:00Added an answer on May 31, 2026 at 1:19 pm

    You really do need to implement Overrides for the object.Equals and object.GetHashCode implementations.

    Basically, implementing IEquatable(of T).Equals by itself will only work so long as the caller KNOWS to call IEquatable(of T).Equals instead of regular object.Equals.

    Consider if you have an ArrayList of Cars and check if the list Contains(myCar), where myCar’s Make and Model are the same as a car in the ArrayList…but the one in the ArrayList isn’t actually the exact same instance. Contains would return false.

    Worse yet, if you had a Hashtable or Dictionary, which uses GetHashCode to determine where to store entries, equality would never work because two cars with the same Make and Model would return different values for GetHashCode()

    Basically, it comes down to you adding the following implementations to car:

    Public Overrides Overloads Function Equals(obj As Object) As Boolean
        Return TypeOf obj Is Car AndAlso Equals(DirectCast(obj, Car)) 
    End Function 
    
    Public Overrides Function GetHashCode() As Int32
        Dim hash As Int32 = 179 ' or something intelligent
    
        hash = hash * 27 + Make.GetHashCode()
        hash = hash * 27 + Model.GetHashCode()
    
        Return hash
    End Function
    

    So the question I have is: why implement IEquatable at all? Why not just override Equals and GetHashCode?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here i saw various question related to usb debugging but answers not really helped
I saw a question here showing to use this: button.Visibility = Visibility.Hidden; but I
I saw this question : XPathDocument vs. XmlDocument But it doesnt have the info
I saw this question , but the answers there are not very relevant. A
I saw a question that is similar to my one but not the same,
I saw many question addressing this issue but still i have some doubts... I
I saw this question already on this forum but I do not know why
I have a question involving the Replace Method. I saw a question similar to
I saw a similar question asked and answered for ASP.net here How do I
I saw another similar question answered here - Velocity editor plugin for Eclipse? .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.