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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:16:42+00:00 2026-05-21T21:16:42+00:00

Suppose I’ve got a generic MyClass<T> that needs to compare two objects of type

  • 0

Suppose I’ve got a generic MyClass<T> that needs to compare two objects of type <T>. Usually I’d do something like …

void DoSomething(T o1, T o2)
{
  if(o1.Equals(o2))
  {
    ...
  }
}

Now suppose my MyClass<T> has a constructor that supports passing a custom IEqualityComparer<T>, similar to Dictionary<T>. In that case I’d need to do …

private IEqualityComparer<T> _comparer;
public MyClass() {}
public MyClass(IEqualityComparer<T> comparer)
{
  _comparer = comparer;
}
void DoSomething(T o1, T o2)
{
  if((_comparer != null && _comparer.Equals(o1, o2)) || (o1.Equals(o2)))
  {
    ...
  }
}

To remove this lengthy if statement, it’d be good if I could have _comparer default to a ‘default comparer’ if the regular constructor is used. I searched for something like typeof(T).GetDefaultComparer() but wasn’t able to find anything like it.

I did find EqualityComparer<T>.Default, could I use that? And would then this snippet …

public MyClass()
{
  _comparer = EqualityComparer<T>.Default;
}
void DoSomething(T o1, T o2)
{
  if(_comparer.Equals(o1, o2))
  {
    ...
  }
}

… provide the same results as using o1.Equals(o2) for all possible cases?

(As a side note, would this mean I’d also need to use any special generic constraint for <T>?)

  • 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-21T21:16:42+00:00Added an answer on May 21, 2026 at 9:16 pm

    It should be the same, but it is not guaranteed, because it depends on implementation details of the type T.
    Explanation:
    Without a constraint to T, o1.Equals(o2) will call Object.Equals, even if T implements IEquatable<T>.
    EqualityComparer<T>.Default however, will use Object.Equals only, if T doesn’t implement IEquatable<T>. If it does implement that interface, it uses IEquatable<T>.Equals.
    As long as T‘s implementation of Object.Equals just calls IEquatable<T>.Equals the result is the same. But in the following example, the result is not the same:

    public class MyObject : IEquatable<MyObject>
    {
        public int ID {get;set;}
        public string Name {get;set;}
    
        public override bool Equals(object o)
        {
            var other = o as MyObject;
            return other == null ? false : other.ID == ID;
        }    
    
        public bool Equals(MyObject o)
        {
            return o.Name == Name;
        } 
    }
    

    Now, it doesn’t make any sense to implement a class like this. But you will have the same problem, if the implementer of MyObject simply forgot to override Object.Equals.

    Conclusion:
    Using EqualityComparer<T>.Default is a good way to go, because you don’t need to support buggy objects!

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

Sidebar

Related Questions

Suppose i have an XML file, that i use as local database, like this):
suppose there is a script doing something like this: # module writer.py import sys
Suppose I have a stream of [acme] objects that I want to expose via
Suppose that I have the following record: type t = {a:int} In order to
Suppose your git history looks like this: 1 2 3 4 5 1–5 are
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
Suppose you're developing a software product that has periodic releases. What are the best
Suppose I'm entertaining a business model that offers free access to my propritary data
Suppose I'm using a placeholder jQuery plugin that reads the placeholder attribute from input

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.