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

  • Home
  • SEARCH
  • 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 357747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:14:04+00:00 2026-05-12T12:14:04+00:00

From MSDN Types that implement IComparable must override Equals.Types that override Equals must also

  • 0

From MSDN

Types that implement IComparable must override Equals.Types that override Equals must also override GetHashCode; otherwise, Hashtable might not work correctly.

I didn’t quite get it. Can anyone explain.

  • 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-12T12:14:04+00:00Added an answer on May 12, 2026 at 12:14 pm

    IComparable is an interface that defines that two instances of the implementing class can be seen as greater than, less than or equal to one another. Since you have defined equality in the methods of that interface you also need to override the Equals method (and equality operator) to ensure the results from the two are consistent.

    public class EqualityTest : IComparable<EqualityTest>
    {
          public int Value { get; set; }
    
          public int CompareTo(EqualityTest other)
          {
               return this.Value.CompareTo(other.Value);
          }
    }
    

    In the above example I have implemented IComparable, but not overridden Equals. If you call CompareTo with two separate instances of the class that have the same Value it will say there are equal. If you call Equals with the same two instances it will say they are not equal as it will test to see if they are the same object (the default implementation of Equals).

    Two equal items should return the same hash code (which are used for quickly finding items used as keys in hash tables) so if you override Equals then you should also override GetHashCode()


    As an example I just created the following class in my IDE:

    public class EqualityTest
    {
         public string A { get; set; }
         public string B { get; set; }
    }
    

    And ran Resharper’s helpful “Generate Equality” function saying that I wanted both A and B to affect equality. This is the code it created:

        public bool Equals(EqualityTest other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }
    
            if (ReferenceEquals(this, other))
            {
                return true;
            }
    
            return Equals(other.A, A) && Equals(other.B, B);
        }
    
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
            {
                return false;
            }
    
            if (ReferenceEquals(this, obj))
            {
                return true;
            }
    
            if (obj.GetType() != typeof(EqualityTest))
            {
                return false;
            }
    
            return Equals((EqualityTest)obj);
        }
    
        public override int GetHashCode()
        {
            unchecked
            {
                return ((A != null ? A.GetHashCode() : 0)*397) ^ (B != null ? B.GetHashCode() : 0);
            }
        }
    
        public static bool operator ==(EqualityTest left, EqualityTest right)
        {
            return Equals(left, right);
        }
    
        public static bool operator !=(EqualityTest left, EqualityTest right)
        {
            return !Equals(left, right);
        }
    

    So if you are overriding Equals then you should also define all of the above to ensure consistency, if you are implementing IComparable then the same applies.

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

Sidebar

Related Questions

From MSDN: The CancelWaitableTimer function does not change the signaled state of the timer.
From the MSDN article on STAThread: Indicates that the COM threading model for an
From the MSDN documentation: Synchronized supports multiple writing threads, provided that no threads are
I understand the main function of the lock key word from MSDN lock Statement
Where can I find the IIS object schema? All I found from MSDN was
Change Data Capture is a new feature in SQL Server 2008. From MSDN: Change
Quoting from the MSDN Link for ValueType Class In cases where it is necessary
It is unclear to me from the MSDN documentation if I should provide a
I have been playing with the demo code from this msdn article by Jeffrey
Is there an easy way to produce MSDN-style documentation from the Visual Studio XML

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.