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 4341862
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:32:29+00:00 2026-05-21T11:32:29+00:00

From the IEqualityComparer<T> remarks section on MSDN: We recommend that you derive from the

  • 0

From the IEqualityComparer<T> remarks section on MSDN:

  1. We recommend that you derive from the
    EqualityComparer<T> class instead of
    implementing the IEqualityComparer<T>
    interface, because the
    EqualityComparer<T> class tests for
    equality using the
    IEquatable<T>.Equals method instead of
    the Object.Equals method. …

    • I don’t understand the quote’s argument of why we would should prefer to derive from EqualityComparer<T> class instead of implementing IEqualityComparer<T>. It implies that objects implementing IEqualityComparer<T> will test for equality using Object.Equals, but isn’t the whole point of implementing IEqualityComparer<T> when we don’t want to test for equality using Object.Equals or IEquatable<T>.Equals?

    • It also implies that if we derive from EqualityComparer<T>, then derived class will test for equality using IEquatable<T>.Equals method. Again, isn’t the whole point of deriving from EqualityComparer<T> when we don’t want to test for equality using Object.Equals or IEquatable<T>.Equals (since EqualityComparer<T>.Default already test using Object.Equals or IEquatable<T>.Equals)?

  2. … This is consistent with the
    Contains, IndexOf, LastIndexOf, and
    Remove methods of the Dictionary<TKey,
    TValue> class and other generic
    collections.

    • I assume most collections in the .NET library test for default equality of elements (i.e. when users don’t provide their own custom IEqualityComparer<T> objects to these collections) by calling IEquatable<T>.Equals or Object.Equals (depending on whether or not elements of type T implement IEquatable<T>) via EqualityComparer<T>.Default.

    • Why don’t these collections (when testing for default equality) call IEquatable<T>.Equals or Object.Equals directly instead of via EqualityComparer<T>.Default class?

  • 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-21T11:32:29+00:00Added an answer on May 21, 2026 at 11:32 am

    Regarding your first question:

    The remarks section for the IEqualityComparer<T> class doesn’t really seem to be providing a reason for why you should prefer deriving from the abstract class over the interface, it sounds more like a reason why the equality comparer interface exists in the first place. What it says there is practically useless, it’s basically describing what the default implementation is doing. If anything, the “reasoning” they’ve provided here sound more like a guideline of what your comparers could do and is irrelevant to what it actually does.

    Looking at the public/protected interface of the EqualityComparer<T> class, there’s only one redeeming quality, it implements the non-generic IEqualityComparer interface. I think what they meant to say that they recommend deriving from it because EqualityComparer<T> actually implements the non-generic IEqualityComparer interface that way your class may be used where the non-generic comparer is required.

    It does make more sense in the remarks section for IComparer<T>:

    We recommend that you derive from the Comparer<T> class instead of implementing the IComparer<T> interface, because the Comparer<T> class provides an explicit interface implementation of the IComparer.Compare method and the Default property that gets the default comparer for the object.

    I suspect it was supposed to say something similar for IEqualityComparer<T> but some ideas were mixed up and ended up with an incomplete description.


    Regarding your second question:

    A primary goal for the collections found in the library was to be as flexible as possible. One way to get that is to allow custom ways of comparing objects within them by providing a IComparer<T> or IEqualityComparer<T> to do the comparisons. It would be much more easier to get an instance of a default comparer when one was not supplied than it is to do the comparisons directly. These comparers in turn could include the logic necessary to call the appropriate comparisons packaged nicely.

    e.g., The default comparers can determine whether T implements IEquatable<T> and call IEquatable<T>.Equals on the object or otherwise use Object.Equals. Better encapsulated here in the comparer than it is potentially repeated in the collections code.

    Besides, if they wanted to fall back on calling IEquatable<T>.Equals directly, they would have to add a constraint on T that would make this call possible. Doing so makes it less flexible and negates the benefits of providing the comparer in the first place.

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

Sidebar

Related Questions

I want to get distinct items from List in C# by using IEqualityComparer interface.
From such a job, I'm doing: @OnApplicationStart public class Bootstrap extends Job { Foo
From time to time a source file somehow gets two class files, like this:
I have this comparer for my object Tenant public class TenantComparer : IEqualityComparer<Tenant> {
I'm need to hash against a member variable instead of the class, so I
A class inherits from HashSet to get a set of unique objects with custom
How have I to implement IEqualityComparer<DataRow> to remove duplicates rows from a DataTable with
Is it any different from the CLR standpoint to implement IEqualityComparer vs overriding the
From viewing the source code that this code makes it looks like itemValue generate
Being somewhat lazy about implementing lots of IEqualityComparers , and given that I couldn't

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.